1

I'm trying to change the UserControl in my mainwindow. I'm using devpress's poco viewmodels.

The main window displays except for where the loginView should be it displays "LoginViewModel_xxxxxx"

The LoginView.xaml is a UserControl.

I have

MainWindow.xaml

    <Grid Row="1">
        <ContentControl Content="{Binding CurrentViewModel}"/>
    </Grid>

MainWindowViewModel.cs

public virtual object CurrentViewModel { get; set; }

public static MainWindowViewModel Create()
{
   return ViewModelSource.Create(() => new MainWindowViewModel());
}
protected MainWindowViewModel()
{
   CurrentViewModel = LoginViewModel.Create();
}

LoginViewModel.cs

public static LoginViewModel Create()
{
    return ViewModelSource.Create(() => new LoginViewModel());
}

protected LoginViewModel()
{
    //unrelated code
}
mm8
  • 163,881
  • 10
  • 57
  • 88
Krystian
  • 71
  • 14

1 Answers1

1

Try to define a DataTemplate for the LoginViewModel:

<ContentControl Content="{Binding CurrentViewModel}">
    <ContentControl.Resources>
        <DataTemplate DataType="{x:Type local:LoginViewModel}">
            <local:LoginView />
        </DataTemplate>
    </ContentControl.Resources>
</ContentControl>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • 1
    Just a note to whoever uses this; if your `ContentControl` can be any number of different view-model types, you need to have multiple DataTemplates defined in the resources, one for each type. – Bradley Uffner Aug 16 '18 at 09:51