2

我有一个 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>

这些行是:

  1. WrapPanel
  2. WrapPanel
  3. TextBox

如果这很重要。

4

3 回答 3

8

如果我理解正确,您可能会使用Auto然后将MaxHeight属性绑定HeightGrid. 也许是这样的:

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

希望这可以帮助。

于 2012-12-13T13:05:18.137 回答
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>

于 2012-12-13T14:17:42.477 回答
1

您可以使马特的建议更加简单明了。

<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>
于 2018-11-28T10:30:25.550 回答