[英]Define the size of a window based on its inner content
我有一個包含對象列表的窗口,這些對象顯示為一些ItemsControl
。 我想設置窗口:
ItemsControl
2行的高度 您知道如何動態執行此操作嗎?
<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>
先感謝您!
在您的xaml中,為控件添加名稱。 例如,我在這里為邊框添加名稱,
<Border x:Name="MainBorder">
....
</Border>
在后面的代碼中,執行以下操作。
public Constructor()
{
InitializeComponent();
// You can set width height here,
this.Width = MainBorder.ActualWidth;
this. Height = MainBorder.ActualHeight;
}
對於大多數控件,您可以在XAML中將其高度和寬度設置為“自動”,並且其大小將適合其內容。
否則,您可以嘗試Faisal Hafeez解釋的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.