简体   繁体   中英

Star sizing is behaving like Auto?

I'm pretty confused by the behaviour of the WPF Grid control.

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. However, in my actual use case, the button is replaced by a Border containing a ScrollViewer. 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.

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.

Perhaps you can switch to using a DockPanel instead?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM