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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 269k
  • Answers 269k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It may be related to a bug with Git1.6.5.1 on… May 13, 2026 at 1:14 pm
  • Editorial Team
    Editorial Team added an answer Sorry, this works perfectly well. Must have been some strange… May 13, 2026 at 1:14 pm
  • Editorial Team
    Editorial Team added an answer Simple way out is to put the folder name inside… May 13, 2026 at 1:14 pm

Related Questions

I was looking at MEF as an extensibility framework, and I'm pretty much sold,
I currently need to provide a means of adding extensibility in my application. I'm
While l was looking over some questions about MEF, I stumbled onto this particular
I'm trying to figure out an architecture for plugins, but I've never worked with
I was looking at the API documentation for stl vector, and noticed there was

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.