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:
WrapPanel
WrapPanel
TextBox
if this is important.
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.