I am looking for advice on handling states in a WPF MVVM environment.
As an example, assume we have
- A TreeView (or some other ItemsControl) with some items and
- a button that is enabled when an item is selected.
My ViewModel contains a property SelectedItem
that is bound to the SelectedItem
of the TreeView.
When the Button is clicked, we change to "special state": the current SelectedItem
is stored somewhere for later reference (let's call the storing variable FirstSelectedItem
). Then the Selection is cleared, the Button is disabled and the user is supposed to select an item again (can be any item, even the same one). So we get some entry in SelectedItem
. Then the data of the item stored in FirstSelectedItem
is changed depending on SelectedItem
(change some internal properties that are not visible in the view). After this is done, SelectedItem
is restored to contain FirstSelectedItem
, the Button is enabled and the view is in "normal state" again.
I would like to know how you approach such problems in WPF. For me it is important to use the MVVM pattern and use codebehind only to redirect stuff to the ViewModel, and that it is possible to give every UI element a different look and behaviour depending on the current state. In a more complicated example, it might be that some controls become disabled or show up or execute another command than usual when a certain state is current.
For the looks, I think a way would be to have some property State
in the ViewModel. The UI elements can then somehow make their looks dependent on that state (although I don't know how, but I'm sure it's possible).
For the behaviour, I have no idea. As far as I know, a Button can bind to one Command, and only one. Or can the binding be changed depending on State
?
State
is property ofViewModel
and method which executesCommand
is inViewModel
. So Method will change behavior based on theState