I've set up a generic dialog window that takes in a viewmodel and title, to then display according to the window's XAML.
For example, this code sets the title and data context of the window and shows it:
public void ShowWindow(object viewModel, string title)
{
var win = new DialogWindow()
{
Title = title,
DataContext = viewModel
};
win.Show();
}
The window's codebehind contains nothing of relevance, but the XAML for its content presenter looks like:
<ContentPresenter x:Name="DialogPresenter" Content="{Binding}">
<ContentPresenter.Resources>
<DataTemplate DataType="{x:Type i:IMyInterface}" >
<c:MyInterfacesControl/>
</DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
The problem is that the datatemplate doesn't fire, as the viewmodel passed in is an implementation of that interface. However, I want the code to detect whether the viewmodel implements that, and use the template accordingly.
I've thought of one or two ways around the problem by using converters, for example checking whether something is an implementation of a converter parameter.
But is there a simpler way here?
Edit: This is not the same as the question suggested in the comments (Edit 2: that's now gone); I won't know the interface type (many different ones may be passed in), and instead have a data context of type object to work with.