Changing DataTemplates in WPF with DataTriggers

I’ve come across a few times when I want to change the layout and contents of my screen depending on what data I have in my object. I’ve found DataTriggers really useful for this.  Based on a field being populated or not is can switch out the actual template I apply to the ContentTemplate e.g.

    <DataTemplate x:Key="clientDetails">
        <
ContentPresenter x:Name="clientDetailsPresenter"
  
ContentTemplate="{StaticResource clientSummary}"
  
Content="{TemplateBinding Content}" />

        <DataTemplate.Triggers>
            <
DataTrigger Binding="{BindingClient.TFN}"Value="{x:Null}">
                <
SetterTargetName="clientDetailsPresenter"
          
Property="ContentTemplate"
          
Value="{StaticResource clientFullDetails}" />
            </
DataTrigger>
        </
DataTemplate.Triggers>

    </DataTemplate>

The other spot I find this really useful is showing different data in a list when the item is Selected.

 

Technorati Tags: ,,