Limit row's height on both “Auto” and “1*” in WPF

我的未来我决定 提交于 2019-12-31 03:04:12

问题


I have a WPF application which layout consists of 3 rows in a top level Grid.

I want the middle row to use up the space it needs (the maximum space it needs is limited but depends on the width of the window). The bottom row shall use up the remaining space. The tricky part is the top row. Its size can vary depending on a button which toggles the visibility of a large part of the content. I want it to use at most 50% of the height but not more than it really needs. The following XAML describes what I want to accomplish:

    <Grid.RowDefinitions>
        <!-- neither "1*" nor "Auto" fully meets my needs -->
        <RowDefinition Height="Min(1*,Auto)"></RowDefinition>

        <RowDefinition Height="Auto"></RowDefinition>

        <RowDefinition Height="1*"></RowDefinition>
    </Grid.RowDefinitions>

The rows are:

  1. WrapPanel
  2. WrapPanel
  3. TextBox

if this is important.


回答1:


If I understand it right, you could probably use Auto and then bind the MaxHeight attribute to the Height of the Grid. Maybe something like this:

MaxHeightConverter.cs:

public class MaxHeightConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            throw new ArgumentException("MaxHeightConverter expects a height value", "values");

        return ((double)value / 2);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

MyWindow.xaml:

...
xmlns:converters="clr-namespace:MyApp.Namespace"
...
<Window.Resources>
    <converters:MaxHeightConverter x:Key="MaxHeightValue" />
</Window.Resources>

<Grid x:Name="root">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="1*"></RowDefinition>
    </Grid.RowDefinitions>

    <WrapPanel >
        <WrapPanel.MaxHeight>
            <Binding Converter="{StaticResource MaxHeightValue}" ElementName="root" Path="ActualHeight" />
        </WrapPanel.MaxHeight>
    </WrapPanel>
</Grid>
...

Hope this helps.




回答2:


Another way you could do this with only XAML would be binding to a hidden object that is the height you want:

<Grid>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Border Background="White" Visibility="Hidden" x:Name="HalfHeightRow" x:FieldModifier="private" />
    </Grid>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Border Height="1000" Background="Red" MaxHeight="{Binding ActualHeight, ElementName=HalfHeightRow}" />
        <Border Grid.Row="1" Height="100" Background="Green" />
        <Border Grid.Row="2" Background="Blue" />
    </Grid>




回答3:


You could make Matt's suggestion even simpler and clearer.

<Grid>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" x:Name="HalfHeightRow"/>
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
    </Grid>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" MaxHeight="{Binding ActualHeight, ElementName=HalfHeightRow}"/>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Border Height="1000" Background="Red" />
        <Border Grid.Row="1" Height="100" Background="Green" />
        <Border Grid.Row="2" Background="Blue" />
    </Grid>


来源:https://stackoverflow.com/questions/13860046/limit-rows-height-on-both-auto-and-1-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!