0

I created library for Android that makes use of MVVM pattern easier.

In my library I have special ViewManager class responsible for resolving appropriate view for given view model. Let me give you an example:

ViewManager.register(ViewModel.class, View.class);  // ViewModel will be now presented by View.
// Now when, for example, tabbed view item will be "new ViewModel()"; it will automatically be presented by View.class

What would be a proper way to achieve that in WPF, so if i bind my TabControl items to collection of view models, it will automatically resolve appropriate view to display them from my ViewManager?

zduny
  • 2,481
  • 1
  • 27
  • 49

1 Answers1

1

You don't need any of that code to do that in WPF... you can do it quite simply and easily by declaring DataTemplates in the App.xaml file:

<DataTemplate DataType="{x:Type YourViewModelsPrefix:YourViewModel">
    <YourViewsPrefix:YourView />
</DataTemplate>
<DataTemplate DataType="{x:Type YourViewModelsPrefix:YourOtherViewModel">
    <YourViewsPrefix:YourOtherView />
</DataTemplate>
<DataTemplate DataType="{x:Type YourViewModelsPrefix:AnotherViewModel">
    <YourViewsPrefix:AnotherView />
</DataTemplate>

Now whenever the Framework comes across an instance of these view model classes, it will render the associated view. You can display them by having a property of the type of your view model using a ContentControl like this:

<ContentControl Content="{Binding YourViewModelProperty}" />
Sheridan
  • 68,826
  • 24
  • 143
  • 183