MVVM Pattern – Rules for Coupling a ViewModel and a View

mvvmsilverlight

So given the Separation of Concerns, how coupled should the View and ViewModel be?

For example, I want the visibility of a Control in the View to be databound (databinded?) to a flag in the ViewModel.

My first hunch would be to use a boolean value, IsControlVisible that returns true/false. However, in the View, visibility is set by an Enum.

So I have a choice: change the property to an Enum, or use a Converter to convert the bool into the Visibility Enum.

Which is the proper approach when trying to follow idiomatic MVVM?

Best Answer

If the View-Model is only concerned with a control being visible or collapsed, then I'd make it a boolean and use a value converter. If the View-Model also needs to communicate the hidden state, then I'd make it an enum (and probably use the Visibility enum).

So to answer your question, I'd have the View-Model express what it needs (visible/collapsed or visible/collapsed/hidden) and adapt the View to fit with the value converter. Other cases may vary though, as with most things.

Related Topic