簡體   English   中英

Star sizing的行為類似於Auto?

[英]Star sizing is behaving like Auto?

我對WPF網格控件的行為感到很困惑。

這是我能得到的最簡單的再現:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" MinHeight="100" MaxHeight="300" />
            <RowDefinition Height="0.1*" MinHeight="100" />
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Height="200" />
        <Button Grid.Row="1" />
    </Grid>
</Window>

如果你運行它並縮小窗口,你會發現在頂部按鈕開始縮小之前,底部按鈕會被剪裁。

您可以通過從按鈕中刪除Height="200"來獲得所需的行為。 但是,在我的實際用例中,該按鈕被包含ScrollViewer的Border替換。 雖然我沒有明確地設置任何一個高度(但我做了一個ScrollViewer的內容),可以看到相同的剪切行為。

那么,問題是:

如何讓行忽略其內容的高度? 或者是否有其他方法可以獲得相同的效果?

您的最小網格大小為300(Row1為200,Row2為100),因此Grid的最小值為300.如果將窗口縮小到該大小以下,則只是剪切Grid並隱藏其中的部分,而不是縮放它。

也許您可以切換到使用DockPanel代替?

<DockPanel>
    <Button DockPanel.Dock="Bottom" MinHeight="100" />
    <Button Grid.Row="0" Height="200" MinHeight="100" MaxHeight="300"  />
</DockPanel>

這樣,您的底部位將始終停靠在屏幕底部,而其他內容占用剩余空間。

如果你真的想保持你的大小比例,我建議使用一個轉換器,將內容控件的高度設置為窗口大小的百分比。

<DockPanel x:Name="Parent">
    <Button DockPanel.Dock="Bottom" MinHeight="100" 
            Height="{Binding ActualHeight,
                             ElementName=Parent, 
                             Converter={StaticResource PercentConverter}, 
                             ConverterParameter=0.1}" />
    <Button Grid.Row="0" Height="200" MinHeight="100" MaxHeight="300"  />
</DockPanel>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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