Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 197845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:53:16+00:00 2026-05-11T16:53:16+00:00

I was looking at MEF as an extensibility framework, and I’m pretty much sold,

  • 0

I was looking at MEF as an extensibility framework, and I’m pretty much sold, except for one point:

Let’s say I want to import both a ViewModel and a View to display it. I think the “right” way to do that is for the MEF part to export a ViewModel class, and a DataTemplate that displays the ViewModel. As an example, say you were building a Visio-like application and you want to import a library of shapes. Each shape needs a View defined in Xaml and a ViewModel that would wrap some underlying Model object.

Is this possible? What would the Import contract look like for the DataTemplate and how do I make WPF aware of the imported DataTemplate?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:53:16+00:00Added an answer on May 11, 2026 at 4:53 pm

    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 resources
    foreach (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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking at Microsoft's Managed Extensibility Framework (MEF) and various IoC containers (such as Unity),
I'm looking for functionality in Java similar to the .NET Managed Extensibility Framework (http://mef.codeplex.com/).
I work with MEF and I am looking how to change the url of
I'm working with MEF right now, but the answer I'm looking for probably is
Looking for a perl one-liner what will find all words with the next pattern:
Looking for a control that allows to select one text value at a time
I'm planning on using MEF within ASP.NET looking for some insight into thread safety
While l was looking over some questions about MEF, I stumbled onto this particular
I'm looking to a framework which will allow me to have a simple plugin
I'm looking to use MEF for a plugin system for an application I'm building.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.