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

时间:2016-07-06 17:51:54

标签: wpf multibinding

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

System.Windows.Data错误:5:BindingExpression生成的值对目标属性无效。 Value = '33 .44444'MultibindingBindingExpression:target元素是'RowDefinition'。 target属性为'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;
    }

谢谢&amp;此致

1 个答案:

答案 0 :(得分:0)

您将从int返回Grid Height Converter。它应该是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值。

返回:

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

从转换器返回GridLength,如下所示,

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