2011-02-10 70 views
0

我有一個網格和一個TreeView的WPF窗口。 Grid的datacontext綁定到樹視圖上的選定項目。但是,因爲並非所有的treeviewitems都適用,所以如果treviewitem不適用,我想禁用網格。所以,我創建了一個值轉換器來執行空檢查並返回一個布爾值。 (在這種情況下,適用的項目不會爲空)WPF網格IsEnabled使用ValueConverter

問題是值轉換器從不使用。我設置了中斷點,他們從未被擊中。我有其他價值轉換器我使用,他們都工作得很好。

有什麼我失蹤了嗎?

<Grid Grid.Column="1" Grid.Row="0" DataContext="{Binding MyVal}" IsEnabled="{Binding MyVal, Converter={StaticResource NullCheckConverter}}" Margin="2,2,2,2"> 

這並不是說這對這個問題很重要,但這裏是ValueConverter代碼:

internal class NullCheckValueConverter : IValueConverter 
{ 
    #region IValueConverter Members 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return !(value == null); 
    } 

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

    #endregion 
} 

回答

5

那是因爲你綁定的DataContext相同的值,你綁定IsEnabled。所以對於IsEnabled它實際上尋找MyVal.MyVal。替換爲:

IsEnabled="{Binding Converter={StaticResource NullCheckConverter}}" 

此外,如果您遇到綁定問題,請在調試模式輸出窗口中檢查綁定錯誤。

+0

DOH !!!!!!!!!!!!!!! – 2011-02-10 20:05:47