I accepted what Josh Smith did in his MSDN article here ... Scroll down to the part, he talks about applying View in the ViewModel. In this case, the View is created during the rendering of the ViewModel. There is no need to manually create a view and then assign a DataContext to the ViewModel. It does this automatically for you.
"You can easily tell WPF how to render a ViewModel using typed DataTemplates. A printed DataTemplate does not have an x: Key value assigned to it, but it does have its DataType property set by an instance of the Type class. If WPF tries to display one of your ViewModel objects, it checks if the resource system has a typed DataTemplate in an area whose DataType is the same (or base class) type of your ViewModel. If it finds one, it uses this template to render the ViewModel referenced by the content property of the tab. "
In other words, you should create your ViewModel as follows:
MyViewModel viewModel = new MyViewModel();
And in your ResourceDictionary you define this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:DemoApp.ViewModel" xmlns:vw="clr-namespace:DemoApp.View" > <DataTemplate DataType="{x:Type vm:MyViewModel}"> <vw:MyView /> </DataTemplate> </ResourceDictionary>
Brent source share