Using MEF to import a WPF DataTemplate? Using MEF to import a WPF DataTemplate? wpf wpf

Using MEF to import a WPF DataTemplate?


Yes, I was able to make this work in the following way:

In my host WPF application, I added this Import:

    [ImportMany("ApplicationResources", typeof(ResourceDictionary))]    public IEnumerable<ResourceDictionary> Views { get; set; }

Then in my composite part, I declared a ViewModel, and a data template for the ViewModel in a regular ResourceDictionary Xaml file. Then I created a code behind for the ResourceDictionary, like this (in this example, the ViewModel is called ItemViewModel and the ResourceDictionary is called ItemView):

[Export("ApplicationResources", typeof(ResourceDictionary))]public partial class ItemView : ResourceDictionary {    public ItemView()    {        InitializeComponent();    }}

For reference, the Xaml for the example ResourceDictionary looks like this:

<ResourceDictionary     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="clr-namespace:MyCompany.MyProduct"    x:Class="MyCompany.MyProduct.ItemView">    <DataTemplate DataType="{x:Type local:ItemViewModel}">        ...    </DataTemplate></ResourceDictionary>

Then, back in my host WPF application, after I successfully compose and before I show the main window, I do this:

// Add the imported resource dictionaries// to the application resourcesforeach (ResourceDictionary r in Views){    this.Resources.MergedDictionaries.Add(r);}

That seems to successfully apply the DataTemplate anywhere WPF sees an ItemViewModel.

EDIT: For anyone who's interested, I released an application framework called SoapBox Core as open source, and it uses this method extensively to import Views into the application resources. It works very well, and you can download the source yourself and take a look at how it works.