How does one “disable” a button in WPF using the MVVM pattern?

前端 未结 5 1587
陌清茗
陌清茗 2020-12-03 10:33

I\'m trying to get a grasp on WPF and MVVM and have been making good progress. The WPF and MVVM side of things are going well.

However, the XAML and data binding sid

5条回答
  •  北海茫月
    2020-12-03 11:15

    By way of using the command pattern. In your view model:

    public class MyViewModel : ViewModel
    {
        private readonly ICommand someCommand;
    
        public MyViewModel()
        {
            this.someCommand = new DelegateCommand(this.DoSomething, this.CanDoSomething);
        }
    
        public ICommand SomeCommand
        {
            get { return this.someCommand; }
        }
    
        private void DoSomething(object state)
        {
            // do something here
        }
    
        private bool CanDoSomething(object state)
        {
            // return true/false here is enabled/disable button
        }
    }
    

    In your XAML:

    
    

    Read this post to find out more about the DelegateCommand.

提交回复
热议问题