2012-06-14 32 views
1

我試圖使用this example來設計TreeView的樣式,並且一切正常,直到我嘗試將綁定添加到我的DataContext。也就是說,我將PathsFill替換爲MultiBinding的箭頭至當前主題。這裏是相對碼:TreeView模板:綁定返回null

<Path x:Name="Collapsed" HorizontalAlignment="Left" VerticalAlignment="Center" 
     Margin="1" Data="M 4 0 L 8 4 L 4 8 Z"> 
    <Path.Fill> 
     <SolidColorBrush> 
      <SolidColorBrush.Color> 
       <MultiBinding Converter="{StaticResource ThemeToColorConverter}" 
          ConverterParameter="Foreground"> 
        <Binding Path="Themes" /> 
        <Binding Path="ThemeIndex" /> 
       </MultiBinding> 
      </SolidColorBrush.Color> 
     </SolidColorBrush> 
    </Path.Fill> 
</Path> 

我已經設置在轉換器中的斷點,這個問題似乎是兩個Bindings找不到自己Paths,因爲它們都發送null到轉換器。不過,我在代碼的早期設置Button樣式時使用了相同的代碼,並且按鈕顯示完美。

我能想到的唯一的事情是,在TreeView Style,Bindings從不同的來源拉。我只是不知道他們會怎麼做或如何解決這個問題。謝謝!

+1

是否有在輸出窗口綁定例外? – Andy

+0

是的,其實。這裏:'System.Windows.Data錯誤:2:找不到目標元素的控制FrameworkElement或FrameworkContentElement。 BindingExpression:路徑=主題;的DataItem = NULL;目標元素是'SolidColorBrush'(HashCode = 31542128);目標屬性是'顏色'(類型'顏色')' – benjer3

+0

好吧,我想我可能會擁有它。 'MultiBinding'試圖從每個'Item'的'Source'拉取。現在如何解決它.... – benjer3

回答

1

好的,我明白了,所以我發佈了我的解決方案,以便其他任何有相同問題的人都可以(希望)找到它。問題是,綁定是綁定到TreeView Items,而不是UserControl's DataContext,所以我命名爲UserControl並設置Bindings' ElementNames到:

<Path x:Name="Collapsed" HorizontalAlignment="Left" VerticalAlignment="Center" 
     Margin="1" Data="M 4 0 L 8 4 L 4 8 Z"> 
    <Path.Fill> 
     <SolidColorBrush> 
      <SolidColorBrush.Color> 
       <MultiBinding Converter="{StaticResource ThemeToColorConverter}" 
          ConverterParameter="Foreground"> 
        <Binding ElementName="Control" Path="Themes" /> 
        <Binding ElementName="Control" Path="ThemeIndex" /> 
       </MultiBinding> 
      </SolidColorBrush.Color> 
     </SolidColorBrush> 
    </Path.Fill> 
</Path>