Onu*_*nur 2 c# wpf grid-layout
我有一个WPF应用程序,其布局由顶层的3行组成Grid.
我希望中间行占用它所需的空间(它需要的最大空间是有限的,但取决于窗口的宽度).底行应占用剩余空间.棘手的部分是顶行.它的大小可以根据按钮切换大部分内容的可见性而变化.我希望它最多使用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>
Run Code Online (Sandbox Code Playgroud)
行是:
WrapPanelWrapPanelTextBox如果这很重要
如果我的理解对不对,你很可能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();
}
}
Run Code Online (Sandbox Code Playgroud)
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>
...
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
小智 7
另一种只使用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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6565 次 |
| 最近记录: |