开发者

WPF grid IsEnabled using ValueConverter

开发者 https://www.devze.com 2023-02-09 15:50 出处:网络
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

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 que开发者_如何转开发stion 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
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消