MVVM bind RelayCommand CanExecute to a Property?

后端 未结 3 954
情歌与酒
情歌与酒 2021-01-14 02:04

I have a Timer and three buttons to control it: Start, Stop, and Pause.
Each button is bound to a RelayCommand.
I have a TimerState property of type enum Timer

3条回答
  •  攒了一身酷
    2021-01-14 02:47

    There doesn't seem to be a better solution. I know what you mean, it seems inelegant but whatever lipstick you put on it, the onus is on the objects involved in the expression to notify the command.

    If your condition is based purely on other notify properties you could add your own handler to PropertyChanged, that provides a bit of abstraction.

    In this case, TimerState would be a VM property. Then you can a handler to your ViewModel.PropertyChanged event. You can then inspect the property name and update your CanExecute. This is still ugly, but at least you have all the garbage in one block.

提交回复
热议问题