Search code examples
c#wpfmvvmuser-controlsdatatemplate

MVVM Displayer OberservableCollection<ViewModel> with unknown UserControl


im a little bit stuck with my current project and hope someone can help me out of this.

My application works with plugins so that the user is able to add additional functionality to the application. Now i would like to have the configuration window in the same style (Maybe a plugin need some kind of configurations).

The configuration window loads all plugins and get the configuration ViewModel from the plugins. All the ViewModels are stored in an ObservableCollection. These ViewModels should be displayed in a TabControl (one tab per ViewModel)

But i dont know the type of UserControl the plugin is using, because the plugin come up with its own UserControl for configuration purposes. Otherwise i would create a TabControl, bind its ItemsSource to the ObservableCollection and specify the UserControl in the Resources (DataTemplates).

But how to do it in case the UserControls are unknown to compile time?

I thought about using a ObservableCollection instead of ViewModels, but im not realy happy with that and even dont know if this will work.

Do you have some idea how to deal with that?

Kind regards,

SyLuS


Solution

  • You could use a ContentControl to achieve this.

    It's used to show views depending on their viewmodel. In your xaml you can specifiy which view should be shown. Based on the viewmodel which is the current DataContext.

    <ContentControl>
        <ContentControl.Resources>
            <DataTemplate DataType="{x:Type vm:MyViewModel}">
                <v:MyView/>
            </DataTemplate>
        </<ContentControl.Resources>
    </ContentControl>
    

    But when you say you are using a plugin system, maybe something like PRISM, you have to setup the datatemplates automatically. Never done this before. But maybe I gave you a point where you can start.