2016-07-06 34 views
0

我正在使用多值轉換器來獲取行的高度。但我在高度綁定(通過snoop看到)上得到以下錯誤。轉換器不能用於行定義高度

System.Windows.Data錯誤:5:由BindingExpression生成的值對目標屬性無效。值= '33 .44444'MultibindingBindingExpression:目標元素是'RowDefinition'。目標屬性是'高度'(類型'GridLength')

即使經過Google搜索,我也無法解決這個問題。任何人都可以請幫我解決這個問題。

我行定義:

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

感謝&問候

+0

是否結合'ActualHeight'任何工作比'Height'更好? 'Height'在運行時通常不會改變,所以''Binding'不會醒來。 –

回答

0

您從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.GridLength由System.Windows.Controls.RowDefinition定義的行的值。

返回:

表示該行的高度System.Windows.GridLength。默認值是1.0。


返回GridLength從您的轉換器像下面,

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