从DataTemplateSelector显式刷新DataTemplate吗? [英] Explicitly refresh DataTemplate from a DataTemplateSelector?

查看:122
本文介绍了从DataTemplateSelector显式刷新DataTemplate吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将ContentControl.DataTemplateSelector设置为所需的内容。

我想要根据命令或其他命令来调用ContentControl,以通过xaml或代码从选择器中重新选择模板。



谢谢

解决方案

我一无所知(非糊涂)的方法:WPF需要选择模板时调用DataTemplateSelector,就WPF而言,这是一次性决定。 (您可以通过使WPF认为内容已更改来模糊它,例如,将内容设置为null然后再次返回-我认为这是可行的,但尚未对其进行测试-但这很丑陋!)



但是,还有另一种方法可以更改的内容显示方式更新以响应数据更改,即通过触发器。您可以在 DataTemplate.Triggers 集合中使用DataTriggers来显示和隐藏元素,具体取决于内容数据。要更改整个显示,您可以例如在Grid中设置两个渲染,并使用触发器控制哪个可见。您甚至可以将数据模板设为ContentControl,并使用触发器更改ContentTemplate。当然,这取决于更改模板是否具有可绑定属性的条件,而情况并非总是如此。



这里简要介绍了选择器与触发器,尽管情况略有不同。


I set up a ContentControl.DataTemplateSelector to my desired one.
I want that according to a command or whatever, call the ContentControl to reselect the template from the selector by either xaml or code.

Thank

解决方案

I'm not aware of any (non-kludgy) way to do this: the DataTemplateSelector is called when WPF needs to select the template, and that's a one-off decision as far as WPF is concerned. (You can kludge it by making WPF think the content has changed, e.g. by setting the content to null and then back again -- I think that would work but haven't tested it -- but this is pretty ugly!) If there is a nice way to do this I too would be interested to know!

However, there is an alternative way to change how content is displayed that does update in response to data changes, and that is through triggers. You can use DataTriggers in your DataTemplate.Triggers collection to show and hide elements depending on the content data. To change the entire display, you could e.g. set up two renderings in a Grid, and use triggers to control which one is visible. You could even make your data template a ContentControl, and use a trigger to change the ContentTemplate. Of course this depends on the criteria for changing the template being bindable properties, which may not always be the case.

Here's some brief discussion of selectors vs. triggers, albeit in a slightly different context.

这篇关于从DataTemplateSelector显式刷新DataTemplate吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆