简体   繁体   English

Star sizing的行为类似于Auto?

[英]Star sizing is behaving like Auto?

I'm pretty confused by the behaviour of the WPF Grid control. 我对WPF网格控件的行为感到很困惑。

Here is the simplest reproduction I could get: 这是我能得到的最简单的再现:

<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>

If you run it, and shrink the window, you will notice that the bottom button gets clipped before the top button begins to shrink. 如果你运行它并缩小窗口,你会发现在顶部按钮开始缩小之前,底部按钮会被剪裁。

You can get the desired behaviour by removing Height="200" from the button. 您可以通过从按钮中删除Height="200"来获得所需的行为。 However, in my actual use case, the button is replaced by a Border containing a ScrollViewer. 但是,在我的实际用例中,该按钮被包含ScrollViewer的Border替换。 Although I don't explicitly set a height on either (but I do one the content of the ScrollViewer), the same clipping behaviour is seen. 虽然我没有明确地设置任何一个高度(但我做了一个ScrollViewer的内容),可以看到相同的剪切行为。

So, the question: 那么,问题是:

How can I get the row to ignore the height of its content? 如何让行忽略其内容的高度? Or is there another way to get the same effect? 或者是否有其他方法可以获得相同的效果?

Your minimum Grid size is 300 (200 for Row1 and 100 for Row2), so the smallest your Grid will ever be is 300. If you shrink the Window below that size, it is simply clipping the Grid and hiding parts of it, and not scaling it. 您的最小网格大小为300(Row1为200,Row2为100),因此Grid的最小值为300.如果将窗口缩小到该大小以下,则只是剪切Grid并隐藏其中的部分,而不是缩放它。

Perhaps you can switch to using a DockPanel instead? 也许您可以切换到使用DockPanel代替?

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

This way your bottom bit will always be docked to the bottom of your screen, while the other content takes up the remaining space. 这样,您的底部位将始终停靠在屏幕底部,而其他内容占用剩余空间。

If you really want to maintain your size ratio, I'd suggest a converter that sets the Height of your content controls to a percentage of the window size. 如果你真的想保持你的大小比例,我建议使用一个转换器,将内容控件的高度设置为窗口大小的百分比。

<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