Click here to Skip to main content
Click here to Skip to main content

MVVM Friendly DataTemplate switching

By , 14 Jun 2011
 
I frequently have a need to switch templates inside listviews. Now, while you could create your own ItemTemplateSelector to choose templates, this just smacks to me of having to add more code to your application than really should be necessary. It would be great if you could do this entirely in XAML.
 
Well, thanks to the great Dr WPF for suggesting it, there is and it's a really simple trick. What you need to do is have your DataTemplate just wrap a Control, and swap in ControlTemplates as necessary.
<DataTemplate x:Key="MyItemTemplate>
  <Control x:Name="myControl" Template="{StaticResource MyMainTemplate}" />
  <DataTemplate.Triggers>
    <DataTrigger Binding="{Binding IsActive} Value="True">
      <Setter TargetName="myControl" Property="Template" Value="{StaticResource MyOtherTemplate}" />
    </DataTrigger>
  </DataTemplate.Triggers>
</DataTemplate>
Now all you need do is add the templates as ControlTemplates instead of DataTemplates and point your listview at this DataTemplate.
 
As you can see, the coding is all done in your XAML and, through the judicious use of data triggers, you can have swappable templates. You've gotta love it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Pete O'Hanlon
CEO
United Kingdom United Kingdom
Member
A developer for more years than I care to remember. I live in the North East of England with 2 wonderful daughters and a wonderful wife.
 
I am not the Stig, but I do wish I had Lotus Tuned Suspension.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThanks a lot for this example - I found two typosmemberRufus Buschart6 Jan '13 - 6:50 
Hi!
 
Thank you for this great solution - I highly appreciate it! There are two missing " characters. One in the first line after 'MyItemTemplate' and one in the line fourth line after 'IsActive'. Maybe you can correct your example Smile | :)
GeneralReason for my vote of 5 coolmemberDaniel Vaughan20 Jun '11 - 11:21 
GeneralRe: I like it mate. The thing I love about XAML is that there ar...protectorPete O'Hanlon23 Jun '11 - 7:36 
GeneralReason for my vote of 5 nicememberMonjurul Habib14 Jun '11 - 11:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 14 Jun 2011
Article Copyright 2011 by Pete O'Hanlon
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid