2015-01-21 45 views
0

我有一個包含對象列表的窗口,其中顯示爲一些ItemsControl。 我想設置窗口:根據其內容定義窗口的大小

  • 時表示
  • 窗口的寬度與行的寬度高度爲2行是ItemsControl

你知道如何動態地做這件事?

<Border> 
    <DockPanel LastChildFill="True" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> 
     <WrapPanel DockPanel.Dock="Bottom" Grid.Row="1"> 
      <Button Content="Confirm" Command="{Binding ConfirmCommand}" /> 
      <Button Content="Cancel" Command="{Binding RejectCommand}" /> 
     </WrapPanel> 
     <ScrollViewer VerticalScrollBarVisibility="Auto"> 
      <ItemsControl Grid.Row="0" ItemsSource="{Binding Path=Confirmations}" ItemTemplate="{StaticResource ConfirmationListTemplate}" HorizontalAlignment="Stretch" /> 
     </ScrollViewer> 
    </DockPanel> 
</Border> 

預先感謝您!

回答

0

在您的xaml中,爲控件添加名稱。例如,我在此添加邊框的名稱,

<Border x:Name="MainBorder"> 
    .... 
</Border> 

在您的代碼背後,執行類似操作。

public Constructor() 
{ 
    InitializeComponent(); 
    // You can set width height here, 
    this.Width = MainBorder.ActualWidth; 
    this. Height = MainBorder.ActualHeight; 
} 
0

對於大多數控件,您可以在XAML中將其高度和寬度設置爲「自動」,並且尺寸將根據其內容進行調整。

或者您可以嘗試Faisal Hafeez解釋的方法。

+0

謝謝,但正如問題中所述,我希望將控件的高度限制爲只能從ItemsControl中顯示的幾行 – goul 2015-01-22 05:43:13