2011-10-02 568 views
1

我想根據兩個文本框的文本增加進度欄值。我寫這個XAML,但有錯誤「雙向綁定需要路徑或XPath」當我在做ProgressBar.Value MultiBinding雙向綁定需要路徑或xpath

<Window.Resources> 
    <local:Class1 x:Key="ConverterM"/> 
</Window.Resources> 

<TextBox Height="23" HorizontalAlignment="Left" Margin="157,59,0,0" 
     Name="textBox1" VerticalAlignment="Top" Width="120" /> 
<TextBox Height="23" HorizontalAlignment="Left" Margin="157,108,0,0" 
     Name="textBox2" VerticalAlignment="Top" Width="120" /> 
<ProgressBar Height="24" HorizontalAlignment="Left" Margin="120,160,0,0" 
      Name="progressBar1" VerticalAlignment="Top" Width="243" > 
    <ProgressBar.Value> 
     <MultiBinding Converter="{StaticResource ConverterM}"> 
      <Binding /> 
      <Binding ElementName="textBox1" Path="Text" /> 
      <Binding ElementName="textBox2" Path="Text" /> 
     </MultiBinding> 
    </ProgressBar.Value> 
</ProgressBar> 

值轉換器:

public class Class1 : IMultiValueConverter 
{ 
    public object Convert(object[] values, 
          Type targetType, 
          object parameter, 
          System.Globalization.CultureInfo culture) 
    { 
     if (values[1] != null && values[2]!=null) 
     { 
      if (((string)values[1]).Length==((string)values[2]).Length) 
      { 
       return 5.0; 
      } 
     } 
     else 
     { 
      return 0.0; 
     } 
    } 

    public object[] ConvertBack(object value, 
           Type[] targetTypes, 
           object parameter, 
           System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

回答

2

我認爲<Binding />不必要。嘗試刪除它並更改轉換器中的索引。

0

雙向綁定需要路徑或XPath

這發生在你沒有設置Path=binding。默認WPF binding將採取Path=部分default

爲了避免這種情況,您需要爲您在MultiBinding中指定的每個Binding提供Path。在你的情況下,有一個空的綁定沒有Path定義,這就是爲什麼你有上述錯誤的經驗。

我遇到了同樣的問題,但接受的答案沒有說錯誤是什麼,所以想到分享這個。