Grid.Row动态调整大小

时间:2015-11-09 07:49:33

标签: wpf grid height multibinding

我在MultiBinding上使用MultiValueConverterGrid,以便根据MaxHeight设置每个Grid.Row的{​​{1}}所有ActualHeight s。

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>

UserControl.Resources:

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();
    }
}

我的问题(一个假的问题)是,当<cvc:AvailableHeightConverter x:Key="CalculateMaxHeightConverter" PanelHeight="1080" /> 的{​​{1}}被更改时,我无法弄明白UpdateSourceTrigger的方式。

2 个答案:

答案 0 :(得分:0)

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

  1. ActualHeight没有setter,因此您需要使用Mode =“OneWay”
  2. 将grdRow1的MaxHeight绑定到所有行ActualHeight包括grdRow1本身可能会产生无限循环的大小调整
  3. 尝试以下方法:

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

答案 1 :(得分:0)

行。在尝试各种senarios后,我最终BindingActualHeight没有通知MultiBinding更改。 @christoph注意到(2.)也非常有帮助。所以我最终绑定了TextBox&#39; ActualWidth,如下所示:

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

并从HeightPanelHeight)中减去一行的AvailableHeightConverter

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

(曾经是1080(-52 MinHeight行)