Change Data template dynamically
2024-10-19 19:42:49
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="{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
最新文章
- jquery 自定义click事件执行多次
- POJ2763 Housewife Wind
- [正则表达式]PCRE反向分组引用
- Android UiAutomator快速调试
- hive中分号问题
- IO流03--毕向东JAVA基础教程视频学习笔记
- centos6.5yum方式升级内核
- [转] 你是as3老鸟吗?但是有些你可能目前都不知道的东西
- Ehcache(2.9.x) - Configuration Guide, Configuring Cache
- strings和nm命令
- Visual Studio 使用及调试必知必会
- 零基础2018如何系统地学习python?
- 面试题-一个for循环输出一个棱形
- SQL Server 2017 新功能分享
- cookie路径问题
- vue中 左侧导航条 多个toggleClass
- C#设计模式 ---- 总结汇总
- MessageFormat格式化数字
- linux cpu过高原因及代码定位
- Menubar
热门文章
- [Unity3D]Unity3D游戏开发之跑酷游戏项目解说
- 关于android fragment 某些使用记录
- Atitit Loading 动画效果
- Atitit 图像处理 halcon类库的使用 &#160;范例边缘检测 attilax总结
- 深入理解Linux内核-进程地址空间
- vue.js $refs和$emit 父子组件交互
- sessionStorage 、localStorage、cookie
- mysql_secure_installation
- MediaType是application/x-www-form-urlencoded的接口测试方法
- IOS开发之xib解决自定义CollectionCell问题