0
votes

I have a WPF Application which is using MVVM.

I know that there ways of doing this in XAML but I am working on a plugin architecture and came up with a solution where a plugin exposes it's viewmodel to my plugin host's viewmodel and it's datatemplate. I want to leave the lifetime management of the plugin view up to WPF. I have tried having the plugins expose a UserControl but ran into issues when WPF decided to dispose of my UserControl so I would not reattach it without weird hacky work arounds. I am having issues getting some sort of binding working to where i can bind a control to the data and it's template to my data template.

I have a ViewModel which looks something like:

public class MyViewModel
{
    public DataTemplate SelectedTemplate{ get; set;}
    public object SelectedViewModel {get; set;}
}

The selected template and viewmodel are determined somewhere else in the code but are irrelevant to my question.

My question is how i can bind to a DataTemplate so that I know how to display the data shown in the SelectedViewModel.

The DataTemplate is a DataTemplate created incode which respresents:

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

I have tried:

<UserControl Template="{Binding Path=SelectedTemplate}" 
    Content="{Binding Path=SelectedViewModel"} />

But UserControl expects a control template and not a data template.

1

1 Answers

1
votes
<UserControl>
   <ContentControl Content="{Binding SelectedViewModel}" ContentTemplate="{Binding SelectedTemplate}" />
</UserControl>