首页 > 代码库 > Change Data template dynamically
Change Data template dynamically
1. Attached Property bound to task state. Any change will dynamically set data template.
2. Visual State Manager
A DataTemplateSelector
does not respond to PropertyChange
notifications, so it doesn‘t get re-evaluated when your properties change.
The alternative I use is DataTriggers
that changes the Template
based on a property.
For example, this will draw all TaskModel
objects using a ContentControl
, and the ContentControl.Template
is based on the TaskStatus
property of the TaskModel
<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=http://www.mamicode.com/"{StaticResource OpenTaskTemplate}" /> <!-- Triggers to change Template --> <Style.Triggers> <DataTrigger Binding="{Binding TaskStatus}" Value=http://www.mamicode.com/"Closed"> <Setter Property="ContentTemplate" Value=http://www.mamicode.com/"{StaticResource ClosedTaskTemplate}" /> </DataTrigger> </Style.Triggers> </Style> </ContentControl.Style> </ContentControl> </DataTemplate>
From: https://stackoverflow.com/questions/13136816/change-data-template-dynamically
Change Data template dynamically
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。