WPF MVVM: Convention over Configuration for ResourceDictionary?

Rather than writing code to explicitly add things to the ResourceDictionary, how about just generating the right view on demand? You can do this with a ValueConverter.

Your resources would look like this:

<Views:ConventionOverConfigurationConverter x:Key="MyConverter"/>
<DataTemplate DataType="{x:Type ViewModels:ViewModelBase}">
    <ContentControl Content="{Binding Converter={StaticResource MyConverter}}"/>
</DataTemplate>

You still need a DataTemplate resource, but as long as your ViewModels all have a common base class, you’ll only need one DataTemplate to take care of all of them.

Then define the value converter class:

public class ConventionOverConfigurationConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
        CultureInfo culture)
    {
        // value is the ViewModel. Based on its GetType(), build a string
        // with the namespace-qualified name of the view class, then:
        return Activator.CreateInstance(Type.GetType(viewName));
    }
    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

All you’d need to do is write the logic inside Convert, which will depend on things like whether your Views and ViewModels are in the same namespace or not.

Leave a Comment