簡體   English   中英

根據窗口的內部內容定義窗口的大小

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM