We’ve had the discussion at work a few times about when to use a property in a ViewModel vs when to use a Converter.
Use a Converter when:
- it can be re-used in other places (a great example is an IsVisibilityConverter)
- it’s a UI related action such as selecting toggle button based on a parameter value
- a parameter isn’t needed or can be a static value
Use a ViewModel property when:
- The value depends on another property (example: duration shown in seconds depends on what the user chose in acombo box, hours, minutes, seconds)
- It needs to be unit tested
- PropertyChanged events can change its value
EDIT: I should mention that I work mainly in Silverlight. So I don’t have multibinding converters out of the box and binding to the parameter doesn’t work (which I think it does in WPF).
What are your thoughts?
In general, I try to think of
IValueConverteras a “pure view” thing. In this case, it should be reusable view code. (You can also useIMultiValueConverterto handle many of the scenarios where you’d need “parameters”, provided it’s still pure view related…)The ViewModel, on the other hand, is application specific logic. If something relies on state that’s specific to your domain, I’d rather put it directly inside of the ViewModel.