[UWP/MVVM]Enable/Disable Button in RadDataGrid Data Template Column that have commands bound to them upon conditions

后端 未结 2 526
孤城傲影
孤城傲影 2020-12-21 22:27

I have set a bool property and have bound it to the IsEnabled in the xaml but the ICommand CanExecute method overrides the IsEnabled in xaml, so my bool property is ineffect

2条回答
  •  一生所求
    2020-12-21 23:13

    I think you can pick a lot of code from the RelayCommand of MVVMLight. Try to change your event to

        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (canExecute != null)
                {
                    CommandManager.RequerySuggested += value;
                }
            }
    
            remove
            {
                if (canExecute != null)
                {
                    CommandManager.RequerySuggested -= value;
                }
            }
        }
    

    and add also a function

        public void RaiseCanExecuteChanged()
        {
            CommandManager.InvalidateRequerySuggested();
        }
    

    Then, whatever you put as your Predicate on the command, at the Predicate's boolean setter do:

    SomeCustomCommand.RaiseCanExecuteChanged()
    

    Hope I helped.

提交回复
热议问题