将行的高度限制为两个“自动"和"1 *"在WPF中 [英] Limit row's height on both "Auto" and "1*" in WPF

查看:93
本文介绍了将行的高度限制为两个“自动"和"1 *"在WPF中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,其布局由顶层Grid中的3行组成.

I have a WPF application which layout consists of 3 rows in a top level Grid.

我希望中间行用完所需的空间(所需的最大空间有限,但取决于窗口的宽度). 最下面一行应将剩余空间用完. 棘手的部分是第一行. 它的大小可能会有所不同,具体取决于按钮,该按钮可切换大部分内容的可见性.我希望它最多使用高度的50%,但不超过实际需要的高度. 以下XAML描述了我要完成的工作:

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>

行是:

  1. WrapPanel
  2. WrapPanel
  3. TextBox
  1. WrapPanel
  2. WrapPanel
  3. TextBox

如果这很重要.

推荐答案

如果我理解正确,则可以使用Auto,然后将MaxHeight属性绑定到GridHeight.也许是这样的:

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:

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:

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>
...

希望这会有所帮助.

这篇关于将行的高度限制为两个“自动"和"1 *"在WPF中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆