Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 700
Onur Avatar asked Dec 18 '25 07:12

Onur


2 Answers

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.

like image 120
JGaarsdal Avatar answered Dec 19 '25 20:12

JGaarsdal


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>

like image 31
Matt Holcomb Avatar answered Dec 19 '25 20:12

Matt Holcomb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!