wpf 动态更改数据模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13136816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Data template dynamically
提问by Tilak
I have a item control which is bound to Tasks. Each task has task state. I have defined different data templates for each task state, and also data template selector.
我有一个绑定到任务的项目控件。每个任务都有任务状态。我为每个任务状态定义了不同的数据模板,还有数据模板选择器。
Problem is that I am not able to figure out how to trigger data template selector when task state is changed dynamically.
问题是我无法弄清楚在动态更改任务状态时如何触发数据模板选择器。
I want to know how to use data triggers together with data templates.
我想知道如何将数据触发器与数据模板一起使用。
If this will not work out, i will explore other alternatives such as
1. Attached Property bound to task state. Any change will dynamically set data template.
2. Visual State Manager
如果这行不通,我将探索其他替代方案,例如
1. 绑定到任务状态的附加属性。任何更改都会动态设置数据模板。
2. 视觉状态管理器
回答by Rachel
A DataTemplateSelector
does not respond to PropertyChange
notifications, so it doesn't get re-evaluated when your properties change.
ADataTemplateSelector
不响应PropertyChange
通知,因此在您的属性更改时不会重新评估它。
The alternative I use is DataTriggers
that changes the Template
based on a property.
我使用的替代方法是根据属性DataTriggers
更改Template
。
For example, this will draw all TaskModel
objects using a ContentControl
, and the ContentControl.Template
is based on the TaskStatus
property of the TaskModel
例如,这将吸引所有TaskModel
使用的对象ContentControl
,并且ContentControl.Template
是基于TaskStatus
财产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>
回答by Noth
Just a quick note for anyone that found this incredibly helpful as I did -
对于像我一样发现这非常有用的任何人,这只是一个简短的说明-
Currently with WPF, it looks like you'll want to use DataTyperather than TargetTypeon your DataTemplate definitions:
目前使用 WPF,您似乎希望在 DataTemplate 定义中使用DataType而不是TargetType:
<DataTemplate x:Key="OpenTaskTemplate" DataType="{x:Type local:TaskModel}">
<TextBlock Text="I'm an Open Task" />
</DataTemplate>
<DataTemplate x:Key="ClosedTaskTemplate" DataType="{x:Type local:TaskModel}">
<TextBlock Text="I'm a Closed Task" />
</DataTemplate>