Grid.Row 动态调整大小

Grid.Row Dynamic Resize

我在 Grid 上使用 MultiBindingMultiValueConverter 以便根据 [=19] 设置每个 Grid.RowMaxHeight =] 共 Grid.Row 个。

               <RowDefinition x:Name="grdRow1" Height="Auto" >
                    <RowDefinition.MaxHeight>
                        <MultiBinding Converter="{StaticResource CalculateMaxHeightConverter}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                            <Binding Path="ActualHeight" ElementName="grdRow1" />
                            <Binding Path="ActualHeight" ElementName="grdRow2" />
                            <Binding Path="ActualHeight" ElementName="grdRow3" />
                            <Binding Path="ActualHeight" ElementName="grdRow4" />
                            <Binding Path="ActualHeight" ElementName="grdRow5" /> 
                        </MultiBinding>
                    </RowDefinition.MaxHeight>
                </RowDefinition>

多转换器:

public class AvailableHeightConverter : IMultiValueConverter
{
    public double PanelHeight { get; set; }

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values == null)
            return Binding.DoNothing;

        double currentHeight = 0d;
        double availableHeight = 0d;

        foreach (object value in values)
        {
            currentHeight += (double)value;
        }

        availableHeight = PanelHeight - currentHeight;

        if (availableHeight < 0)
            return Binding.DoNothing;

        return availableHeight;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

UserControl.Resources:

<cvc:AvailableHeightConverter x:Key="CalculateMaxHeightConverter" PanelHeight="1080" />

我的问题(一个虚拟的问题)是每当 Grid.RowActualHeight 被更改时,我无法弄清楚如何 UpdateSourceTrigger

你的xaml有两个问题(据我所知,没有亲自尝试)

  1. A​​ctualHeight没有setter,所以需要使用Mode="OneWay"
  2. 将 grdRow1 的 MaxHeight 绑定到包括 grdRow1 本身在内的所有行 ActualHeight 可能会产生无休止的调整大小循环

尝试以下操作:

            <RowDefinition x:Name="grdRow1" Height="Auto" >
                <RowDefinition.MaxHeight>
                    <MultiBinding Converter="{StaticResource CalculateMaxHeightConverter}" Mode="OneWay" UpdateSourceTrigger="PropertyChanged">
                        <Binding Path="ActualHeight" ElementName="grdRow2" Mode="OneWay" />
                        <Binding Path="ActualHeight" ElementName="grdRow3" Mode="OneWay" />
                        <Binding Path="ActualHeight" ElementName="grdRow4" Mode="OneWay" />
                        <Binding Path="ActualHeight" ElementName="grdRow5" Mode="OneWay" /> 
                    </MultiBinding>
                </RowDefinition.MaxHeight>
            </RowDefinition>

好的。在尝试了各种 senarios 之后,我发现 Binding 行的 ActualHeight 没有通知 MultiBinding 进行更改。 @christoph 注意到的 (2.) 也非常有帮助。所以我最终绑定了 TextBoxActualWidth,如下所示:

第 1 行

            <RowDefinition x:Name="grdRow1" Height="Auto" >
            <RowDefinition.MaxHeight>
                <MultiBinding Converter="{StaticResource CalculateMaxHeightConverter}">
                    <Binding Path="ActualHeight" ElementName="txtBox2" Mode="OneWay" />
                    <Binding Path="ActualHeight" ElementName="txtBox3" Mode="OneWay" />
                    <Binding Path="ActualHeight" ElementName="txtBox4" Mode="OneWay" />
                    <Binding Path="ActualHeight" ElementName="txtBox5" Mode="OneWay" /> 
                </MultiBinding>
            </RowDefinition.MaxHeight>
        </RowDefinition>

第 2 行

        <RowDefinition x:Name="grdRow2" Height="Auto" >
            <RowDefinition.MaxHeight>
                <MultiBinding Converter="{StaticResource CalculateMaxHeightConverter}">
                    <Binding Path="ActualHeight" ElementName="txtBox1" Mode="OneWay" />
                    <Binding Path="ActualHeight" ElementName="txtBox3" Mode="OneWay" />
                    <Binding Path="ActualHeight" ElementName="txtBox4" Mode="OneWay" />
                    <Binding Path="ActualHeight" ElementName="txtBox5" Mode="OneWay" /> 
                </MultiBinding>
            </RowDefinition.MaxHeight>
        </RowDefinition>

并从 AvailableHeightConverterPanelHeight 中减去一行的 Height

<cvc:AvailableHeightConverter x:Key="CalculateMaxHeightConverter" PanelHeight="1028" />

(以前是 1080(-52 MinHeight 一行)