I’m trying to change a DataTemplate for objects in my WPF application according to a specific boolean value. When the value is “True” I want the DataTemplate to be something and when the value is “False” I want the DataTemplate to be something else.
I’ve tried writing this code, but so far I end up with an annoying “Out of Memory exception”.
<DataTemplate DataType="{x:Type vm:MyObjectViewModel}">
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Visible}" Value="False">
<Setter TargetName="MainTemplateGrid" Property="Content">
<Setter.Value>
<Ellipse Width="50" Height="50" Fill="Red" />
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Visible}" Value="True">
<Setter TargetName="MainTemplateGrid" Property="Content">
<Setter.Value>
<Image Source="{Binding Icon}" Opacity="{Binding Visible, Converter={StaticResource VisibilityConverter}}" />
</Setter.Value>
</Setter>
</DataTrigger>
</DataTemplate.Triggers>
<ContentControl x:Name="MainTemplateGrid" />
</DataTemplate>
If anyone has a clue on how to fix this, please let me know.
I figured it out.
I did this using a
ContentControland by setting itsStyleusing theDataTriggers, here’s the code: