Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

WPF and MVVM: How to inject ViewModel into Child control with MEF?

$
0
0

I have user control and its associated Viewmodel namely, ChildView and ChildViewModel. Code is as follows,

public partial class ChildView
    {
        public ChildView()
		{
		   InitializeComponent();
		}

                [Import]
		public ChildViewModel Model
		{
			get { return this.DataContext as ChildViewModel; }
			set { this.DataContext = value; }
		}
    }


Now, i want to use ChildView in MainWindow of another assembly. Sample code is as below,

<Window x:Class="Test.MainWindow" ... ><Grid><externalAssembly:ChildView/></Grid></Window>

this creates an instance of ChildView, it is fine. But it is not injecting ChildViewModel hence, getting Model property in view as NULL always.

How can I instantiate and inject ViewModels both to the child view and the MainWindow using MEF?




Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>