WPF grid IsEnabled using ValueConverter

2.8k views Asked by At

I have a WPF window with a Grid and a TreeView. The datacontext for the Grid is bound to the selected item on the treeview. However, because not all treeviewitems are applicable, I want to disable the grid if the treviewitem isn't applicable. So, I created a value converter to do a null check and return a bool. (Applicable items would not be null in this case)

The problem is that the value converter is never used. I set break points and they are never hit. I have other value converters I'm using and they all work just fine.

Is there something I'm missing?

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

Not that it's important for this question but here is the ValueConverter code:

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

There are 1 answers

0
Andrii On BEST ANSWER

That's because you bind DataContext to the same value as you binding IsEnabled. So for IsEnabled it actually looking for MyVal.MyVal. Replace to:

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

Also further if you have issues with binding, check in debug mode output window for binding errors.