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 7885559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:06:11+00:00 2026-06-03T05:06:11+00:00

I am writing a WPF application using MEF and a third-party library called MEFedMVVM

  • 0

I am writing a WPF application using MEF and a third-party library called MEFedMVVM.

I am attempting to create a design whereby a parent view model has a collection of child view models, and I wish to use the view-model-first approach as this keeps the views outside of the view models thereby keeping the code more view model-centric and more unit testable.

I have read this discussion and this discussion regarding using DataTemplate for the view, and also Reed Copsy, Jr’s suggestion here to use a generic view to view model mapping resource. But, I’m struggling to actually implement something that works.

My parent view is very simple:

<UserControl x:Class="MyParentView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:meffed="clr-namespace:MEFedMVVM.ViewModelLocator;assembly=MEFedMVVM.WPF"
             meffed:ViewModelLocator.ViewModel="MyParentViewModel" />

The parent view model derives from a base type that implements IContextAware:

[ExportViewModel("MyParentViewModel")]
public class MyParentViewModel : ViewModelBase
{
    [ImportingConstructor]
    public MyParentViewModel()
    {
        var myChildVM = ServiceLocator.Current.GetInstance<MyChildViewModel>());
    }
}

This is the child view model:

[Export(typeof(MyChildViewModel))]
[ExportViewModel("MyChildViewModel", true)]
public class MyChildViewModel : ViewModelBase
{
}

And this has a corresponding view:

<UserControl x:Class="MyChildView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:meffed="clr-namespace:MEFedMVVM.ViewModelLocator;assembly=MEFedMVVM.WPF"
             meffed:ViewModelLocator.ViewModel="MyChildViewModel" />

Initially, I thought that specifying the second Boolean parameter on ExportViewModel attribute for MyChildViewModel would make everything work using a view-model-first approach as my views and view models are MEFed together in the views’ XAML code. However, turns out this is not the case, and what I actually get passed in to IContextAware.InjectContext() when I instantiate a MyChildViewModel object in the MyParentViewModel constructor is a MyParentView object. Not a MyChildView object as I was expecting and hoping. Clearly, I need to add something to wire them up together. Could anyone provide an example on how to do this?

Thanks!

  • 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-06-03T05:06:12+00:00Added an answer on June 3, 2026 at 5:06 am

    When you really want to use view-model-first then you should do:

    [ExportViewModel("MyParentViewModel")]
    public class MyParentViewModel : ViewModelBase
    {
        // Create property for your child vm
        public MyChildViewModel Child {get; private set}
    
        // If you do MEF use constructor injection instead of servicelocator
        [ImportingConstructor]
        public MyParentViewModel(MyChildViewModel child)
        {
            this.Child = child;
        }
     }
    

    then define a datatemplate for your childvm

     <DataTemplate DataType="{x:Type local:MyChildViewModel}">
         <view:MyChildViewUserControl />
     </DataTemplate>
    

    in your MainView you know where you want to show the child data, otherwise you wouldn’t need the child property 😉 so simply put a ContentControl where the Child data should go and bind to your property.

    e.g.

    <TabControl>
       <TabItem Header="MyChildData">
          <ContentControl Content="{Binding Child}" />
       </TabItem>
    </TabControl>
    

    PS: Code is written without IDE, so errors possible 🙂

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

Sidebar

Related Questions

I am writing .NET3.5, WPF application using Composite Application Library. Application is divided into
I'm writing an application using WPF, and I need to make something that looks
I'm working on writing a small application using the WPF MediaKit , and I
I am writing a WPF application using Self-Tracking Entities. I'm having trouble Disabling/Enabling my
I'm writing a WPF application using the MVVM pattern, based on the following article:
I'm writing an application in WPF using the MVVM-pattern and will really often use
I'm writing a WPF application while mostly adhering to the MVVM design pattern. The
I'm very new to WPF and am writing an application using this example as
I am writing a WPF application using C# and I need some help with
I'm writing WPF application, that's using MVVMLight. I have a DataGrid and I wanna

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.