Click here to Skip to main content
14,983,344 members
Articles / Desktop Programming / WPF
Posted 14 Jun 2011

Tagged as


22 bookmarked

MVVM Friendly DataTemplate switching

Rate me:
Please Sign up or sign in to vote.
4.94/5 (16 votes)
29 Jul 2013CPOL
Switching data templates entirely in XAML.
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}" />
    <DataTrigger Binding="{Binding IsActive}" Value="True">
      <Setter TargetName="myControl" Property="Template" Value="{StaticResource MyOtherTemplate}" />
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.


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
United Kingdom United Kingdom
A developer for over 30 years, I've been lucky enough to write articles and applications for Code Project as well as the Intel Ultimate Coder - Going Perceptual challenge. 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.

Comments and Discussions

QuestionWorking example Pin
SissiSaatana13-May-17 10:29
MemberSissiSaatana13-May-17 10:29 
QuestionCan't get it to work Pin
MrFission18-Mar-16 0:09
MemberMrFission18-Mar-16 0:09 
AnswerRe: Can't get it to work Pin
Pete O'Hanlon18-Mar-16 0:38
mvePete O'Hanlon18-Mar-16 0:38 
QuestionGood stuff question re: the bindings Pin
mwpowellhtx18-Dec-14 10:00
Membermwpowellhtx18-Dec-14 10:00 
AnswerRe: Good stuff question re: the bindings Pin
Pete O'Hanlon18-Dec-14 10:33
mvePete O'Hanlon18-Dec-14 10:33 
QuestionThanks a lot for this example - I found two typos Pin
Rufus Buschart6-Jan-13 6:50
MemberRufus Buschart6-Jan-13 6:50 
AnswerRe: Thanks a lot for this example - I found two typos Pin
Pete O'Hanlon29-Jul-13 6:16
mvePete O'Hanlon29-Jul-13 6:16 
GeneralReason for my vote of 5 cool Pin
Daniel Vaughan20-Jun-11 11:21
MemberDaniel Vaughan20-Jun-11 11:21 
GeneralRe: I like it mate. The thing I love about XAML is that there ar... Pin
Pete O'Hanlon23-Jun-11 7:36
mvePete O'Hanlon23-Jun-11 7:36 
GeneralReason for my vote of 5 nice Pin
Monjurul Habib14-Jun-11 11:13
professionalMonjurul Habib14-Jun-11 11:13 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.