The easiest way is when the difference can be distinguished by a property, which can be checked through a DataTrigger
. For example, if there was a TypeId
property which was a string or enum with values foo
and bar
, a style like this could be applied to the image:
<Style TargetType="Image">
<Setter Property="Source" Value="foo.png" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=TypeId}" Value="bar">
<Setter Property="Source" Value="bar.png" />
</DataTrigger>
</Style>
</Style>
Another way is to take advantage of the fact that DataTemplate
resources defined with a DataType
will automatically be applied to that type. But instead of having global resources that re-implement your entire template for each type, you could place a ContentControl
within your main template, with its own local resources for just the image part:
<ContentControl Grid.Column="0" Content="{Binding}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type viewModels:foo}">
<Image Source="foo.png" />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:bar}">
<Image Source="bar.png" />
</DataTemplate>
</ContentControl.Resources>
</ContentControl>