Should your ViewModel expose XAML elements as properties or not?

mvvm, silverlight, valueconverter, wpf, xaml

Solution

Because then that limits the ViewModel to be used only with a specific visual representation. Once you have the ViewModel emitting XAML, it puts design content into a developer's domain. This means that the designer using Expression Blend cannot edit design assets - and the designer/developer workflow is broken. Keeping the XAML on the page and using Value converters with data templating keeps the design separated from the code.

When your ViewModel exposes specific XAML it also limits that ViewModel to be used only in that specific instance and makes it less reusable.

Problem

Over at the StackOverflow question How can WPF Converters be used in an MVVM pattern? I've learned that Value Converters should not be used in the MVVM pattern since the functionality of a Value Converter should be handled by the ViewModel itself. This makes sense. But I remember reading that you should not expose XAML elements to the View, but instead expose only collections of data which the View then binds and displays using DataTemplates. However, converters seem quite powerful (e.g. as they are used in the MVVM Template demo, see the "Messenger Sample" after unpacking it) in that they can convert objects to objects, e.g. Message objects to FlowDocument objects, or Customer objects into Visibility objects, or custom Status objects into Images, etc. So if a ViewModel is going to take on the functionality of a Value Converter, it is going to have to expose XAML elements and properties such as StackPanel, Visibility, Color, FlowDocument, etc., right? Does anyone see any reason why a ViewModel should not expose these rich XAML objects as Value Converters do?

Original source

Related problems