【问题标题】:Grid.Row Dynamic ResizeGrid.Row 动态调整大小
【发布时间】:2023-03-14 02:18:01
【问题描述】:

我在Grid 上使用MultiBindingMultiValueConverter,以便根据所有Grid.Rows 中的ActualHeight 设置每个Grid.RowMaxHeight

               <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

【问题讨论】:

  • 尝试将接口 INotifyPropertyChanged 实现到您的类。
  • 这会在某些属性发生变化时自动通知 UI
  • 但在 MultiValueConverter 中不需要
  • 但是您需要一些东西来触发 UpdateSource 吗?不是吗?
  • 是的,但是,IMultiValueConverter 应该能够检测到作为values[] 传递的Bindings 的任何更改

标签: wpf grid height multibinding


【解决方案1】:

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

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

尝试以下方法:

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

【讨论】:

  • ActualHeight 更改时仍未更新。我认为这与ActualHeight Property 的只读问题有关,如here 所述。
【解决方案2】:

好的。在尝试了各种场景之后,我发现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>

并从PanelHeightAvailableHeightConverter)中减去一行的Height

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

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

【讨论】:

    猜你喜欢
    • 2013-09-14
    • 2012-10-24
    • 2014-03-19
    • 2015-11-02
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2012-07-29
    • 2012-12-01
    相关资源
    最近更新 更多