2015-01-21 99 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