how to load wpf usercontrol in MVVM pattern

mvvm, user-controls, wpf

Solution

You can use a `ContentControl`, with a `DataTemplate` to bind the `UserControl` (View) to the ViewModel :

<DataTemplate DataType="{x:Type vm:MyViewModel}">
    <v:MyUserControl />
</DataTemplate>

...

<ContentControl Content="{Binding Current}" />

WPF will pick the `DataTemplate` automatically based on the type of the `Content`

Problem

I'm creating a wpf user control which is in mvvm pattern. So we have : view(with no code in codebehind file), viewmodel,model,dataaccess files. I have MainWindow.xaml as a view file, which I need to bind with MainWindowModel.cs. Usually, in a a wpf application we can do this with onStartUp event in App.xaml file. But in user control, as we do not have App.xaml...How do I achieve it ? Please help :(...Thanks in Advance !!!

Original source