3

Let's say we have a view which has a view model as data context. It is binded to a property called Visible.

What type should the property be ?

  • Boolean (more model friendly, but forces the use of a converter) ?
  • Visibility (more view friendly) ?
David Khuu
  • 937
  • 3
  • 10
  • 21

4 Answers4

4

Leave the bool value in the ViewModel and use a BoolToVisibilityConverter in the View.

Reason:

ViewModel should be View-agnostic, and UI-Framework-agnostic. That is, you should be able to copy your ViewModel into a console application and hit F5.

Make sure you leverage MarkupExtension to simplify converter usage

Community
  • 1
  • 1
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
4

To the title question, in general: Closer to the View.

To the example: Use a boolean.

Your VM should be the logical model of the View. But it should not contain any GUI related types (Visibility).

H H
  • 263,252
  • 30
  • 330
  • 514
3

I think of the view model as being the representative of the application workflow. In general a given application should have a view model per view - but that doesn't mean that the view model should in any way be coupled with a specific view - I think of it as the view model matches up with a theoretical view at some point in the application's workflow.

View models should ABSOLUTELY NOT expose UI-specific types (i.e. Visibility, Image etc.). Keep your view model UI Agnostic and use value converters to convert general types to UI Specific ones.

It should NOT be representative of the model unless the model happens to be closely represented by the view you are looking at.

Frank Caico
  • 363
  • 2
  • 8
2

You should go for Boolean. It is also more flexible, i.e. if you want to bind this property to Checkbox you could easily do this without requiring to change any ViewModel Code and gives your UI guy more freedom to decide how the value should be bound in the UI.

Tseng
  • 61,549
  • 15
  • 193
  • 205