限制 WPF 中“自动”和“1*”的行高
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13860046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Limit row's height on both "Auto" and "1*" in WPF
提问by Onur
I have a WPF application which layout consists of 3 rows in a top level Grid.
我有一个 WPF 应用程序,它的布局在顶层包含 3 行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:
我希望中间行用完它需要的空间(它需要的最大空间是有限的,但取决于窗口的宽度)。底行将用完剩余空间。棘手的部分是顶行。它的大小可以根据切换大部分内容可见性的按钮而变化。我希望它最多使用 50% 的高度,但不要超过它真正需要的高度。以下 XAML 描述了我想要完成的任务:
<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:
这些行是:
WrapPanelWrapPanelTextBox
WrapPanelWrapPanelTextBox
if this is important.
如果这很重要。
回答by JGaarsdal
If I understand it right, you could probably use Autoand then bind the MaxHeightattribute to the Heightof the Grid. Maybe something like this:
如果我的理解对不对,你很可能Auto再绑定的MaxHeight属性到Height的Grid。也许是这样的:
MaxHeightConverter.cs:
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:
我的窗口.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.
希望这可以帮助。
回答by Matt Holcomb
Another way you could do this with only XAML would be binding to a hidden object that is the height you want:
仅使用 XAML 执行此操作的另一种方法是绑定到您想要的高度的隐藏对象:
<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>
回答by kphil80
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>

