1. Attached Property bound to task State. Any change will dynamically set data template.
2. Visual State Manager
ADataTemplateSelector
Does not respondPropertyChange
Events, so it doesn' t get re-evaluated when your properties change.
The alternative I use isDataTriggers
That changesTemplate
Based on a property.
For example, this will draw allTaskModel
Objects usingContentControl
, AndContentControl.Template
Is based onTaskStatus
Property ofTaskModel
<DataTemplate x:Key="OpenTaskTemplate" TargetType="{x:Type local:TaskModel}"> <TextBlock Text="I‘m an Open Task" /></DataTemplate> <DataTemplate x:Key="ClosedTaskTemplate" TargetType="{x:Type local:TaskModel}"> <TextBlock Text="I‘m a Closed Task" /> </DataTemplate><DataTemplate DataType="{x:Type local:TaskModel}"> <ContentControl Content="{Binding }"> <ContentControl.Style> <Style TargetType="{x:Type ContentControl}"> <!-- Default Template --> <Setter Property="ContentTemplate" Value="{StaticResource OpenTaskTemplate}" /> <!-- Triggers to change Template --> <Style.Triggers> <DataTrigger Binding="{Binding TaskStatus}" Value="Closed"> <Setter Property="ContentTemplate" Value="{StaticResource ClosedTaskTemplate}" /> </DataTrigger> </Style.Triggers> </Style> </ContentControl.Style> </ContentControl> </DataTemplate>
From: https://stackoverflow.com/questions/13136816/change-data-template-dynamically
Change Data Template dynamically