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

后端 未结 2 530
孤城傲影
孤城傲影 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条回答
  •  猫巷女王i
    2020-12-21 22:58

    You can always do your conditional statement within the CanExecute function of your custom command, no need for you to bind IsEnabled property with your button that is bound to a command. Here's a sample implementation, hope this helps.

    Custom Command:

    public class CustomCommand : ICommand
    {
        private readonly Action _action;
        private readonly Predicate _canExecute;
    
        public CustomCommand(Action action, Predicate canExecute)
        {
            _action = action;
            _canExecute = canExecute;
        }
    
        public bool CanExecute(object parameter)
        {
            return _canExecute((T)parameter);
        }
    
        public void Execute(object parameter)
        {
            _action((T)parameter);
        }
    
        public event EventHandler CanExecuteChanged;
    }
    

    As you can see here, I created an object that implements the ICommand interface, this custom command accepts a generic type parameter which is used to evaluate a condition (CanExecute: this tells whether to enable or disable a command (in UI, the button), normally use to check for permissions, and other certain conditions) this parameter is also used to execute the action (Execute: the actual logic/action to be performed), The command contructor accepts delegate parameters that contain signatures for these 2 methods, the caller may choose lambda or standard methods to fillup these parameters.

    Sample ViewModel:

    public class ViewModel1: INotifyPropertyChanged
        {
            public ViewModel1()
            {
                // Test Data.
                Items = new ObservableCollection
                {
                    new ItemViewModel{ Code = "001", Description = "Paint" },
                    new ItemViewModel{ Code = "002", Description = "Brush" },
                    new ItemViewModel{ Code = "003", Description = "" }
                };
    
                EditCommand = new CustomCommand(Edit, CanEdit);
            }
    
            public CustomCommand EditCommand { get; }
    
            private bool CanEdit(ItemViewModel item)
            {
                return item?.Description != string.Empty;
            }
    
            private void Edit(ItemViewModel item)
            {
                Debug.WriteLine("Selected Item: {0} - {1}", item.Code, item.Description);
            }
    
            private ObservableCollection _items { get; set; }
    
            public ObservableCollection Items
            {
                get => _items;
                set
                {
                    _items = value;
                    OnPropertyChanged();
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    

    XAML:

    
        
            
        
        
            
                
                    
                        
                            
                            
                            

提交回复
热议问题