Why is a button enabled if the Command binding resolves to null?

前端 未结 3 1733
别跟我提以往
别跟我提以往 2021-01-19 06:43

OK, the XAML is quite simple and uses MVVM to bind to an ICommand SomeCommand { get; } property on a view model:

3条回答
  •  萌比男神i
    2021-01-19 07:13

    It is enabled because that's the default state. Disabling it automatically would be an arbitrary measure that gives rise to other problems.

    If you want to have a button without an associated command be disabled, bind the IsEnabled property to SomeCommand using an appropriate converter, e.g.:

    [ValueConversion(typeof(object), typeof(bool))]
    public class NullToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value !== null;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

提交回复
热议问题