Disable WPF buttons during longer running process, the MVVM way

后端 未结 4 465
遇见更好的自我
遇见更好的自我 2020-12-16 13:12

I have a WPF/MVVM app, which consists of one window with a few buttons.
Each of the buttons triggers a call to an external device (an USB missile launcher), which takes

4条回答
  •  被撕碎了的回忆
    2020-12-16 13:32

    Ok the CanExecute method will not work because the click will immediately put you into your long-running task.
    So here's how I would do it:

    1. Make your view model implement INotifyPropertyChanged

    2. Add a property called something like:

      public bool IsBusy
      {
          get
          {
              return this.isBusy;
          }
          set
          { 
              this.isBusy = value;
              RaisePropertyChanged("IsBusy");
          }
      }
      
    3. Bind your buttons to this property in this manner:

    4. In your ShowMessage/CallExternal device methods add the line

      IsBusy = true;
      

    Should do the trick

提交回复
热议问题