转换器不适用于行定义高度

Converter not working for row definition height

我正在使用多值转换器来获取行的高度。但是我在高度绑定上遇到以下错误(通过 snoop 看到)。

System.Windows.Data 错误:5:BindingExpression 生成的值对于目标 属性 无效。 Value='33.44444' MultibindingBindingExpression:target 元素是 'RowDefinition'。目标 属性 是 'Height'(类型 'GridLength')

即使谷歌搜索了很多,我也无法解决这个问题。谁能帮我解决这个问题。

我的行定义:

  <Grid.RowDefinitions>
                            <RowDefinition>
                                <RowDefinition.Height>
                                    <MultiBinding 
   Converter="{StaticResource HeightConverter}">
                                        <Binding Path="Height" 
 RelativeSource="{RelativeSource AncestorType=controls:TestControl, 
 Mode=FindAncestor}" UpdateSourceTrigger="PropertyChanged"></Binding>
                                        <Binding Path="MR" 
  RelativeSource="{RelativeSource AncestorType=controls:TestControl,
  Mode=FindAncestor}" UpdateSourceTrigger="PropertyChanged"></Binding>
                                        <Binding Path="BR" 
  RelativeSource="{RelativeSource AncestorType=controls:TestControl, 
  Mode=FindAncestor}" UpdateSourceTrigger="PropertyChanged"></Binding>
                                    </MultiBinding>
                                </RowDefinition.Height>
                            </RowDefinition>
                            <RowDefinition Height="Auto"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                        </Grid.RowDefinitions>

我的身高转换器代码:

    public object Convert(object[] values, Type targetType, object 
    parameter, CultureInfo culture)
    {
        var TH = (double)values[0];
        var TR = (double)values[1];
        var BR = (double)values[2];

        var per = TR + BR;
        var per2 = (TR/per)*100;

        return (int)(per2/TH)*100;
    }

感谢和问候

您正在从您的 Converter 返回 int 用于您的 Grid Height。应该是 GridLength

见下文来自 MSDN

 public System.Windows.GridLength Height { get; set; }
 Member of System.Windows.Controls.RowDefinition

总结:

获取 System.Windows.Controls.RowDefinition 元素的计算高度,或设置由 System.Windows.Controls.RowDefinition.

定义的行的 System.Windows.GridLength 值

Returns:

表示行高的System.Windows.GridLength。默认值为 1.0.


Return 来自您转换器的 GridLength,如下所示,

return new GridLength(0, GridUnitType.Star); // Or
return new GridLength(per2/TH)*100);