如果我理解正确,您可能会使用Auto
然后将MaxHeight
属性绑定Height
到Grid
. 也许是这样的:
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>
...
希望这可以帮助。