问题描述
我为列表框控件定义了2个数据模板。 1个模板用于未选定状态,另一个用于选定状态(显示的详细信息比未选定状态更多。)
我在此遵循以下示例:
http://blogs.msdn.com/b/wpfsdk/archive/2007/04/16/how-do-i-programmatically-
关于如何从后面的代码访问DataTemplates内部的元素。
>我理解正确,但它仅查找并返回UnSelected DataTemplate的元素。但是当我在Selected DataTemplate中搜索一个元素时,我得到了NullReferenceException。
我在做什么错了?
您在做什么错了?
我会说您做错了什么是尝试访问元素顽皮,顽皮!
开玩笑了,我有99.9%的时间看到有人试图访问a中的元素来自代码的DataTemplate,这是因为其应用程序设计不佳,没有(或很少)绑定,也没有视图模型。许多初学者倾向于将其数据直接存储在UI元素中,而不是使用视图模型。我认为这是因为他们的思想被经验丰富的VB,WinForms和ASP.NET破坏了,而这是正常的做法。使用视图模型并绑定数据而不是将其存储在UI元素中有千种原因。在线查找模型视图视图模型以获取更多详细信息。
现在回答您的问题:
任何给定的ListBoxItem一次只能有一个DataTemplate。这是因为其ContentPresenter仅具有一个ContentTemplate属性,并且该属性不能具有两个不同的值。
因此,ListBoxItem下的可视树将始终从以下位置生成一个是特定模板,而不是多个模板的组合。如果您更改ListBox的ItemTemplate或更新ListBoxItem.ContentTemplate,则将丢弃旧模板生成的可视树并构建新的可视树。
让我再说一遍:如果更改数据模板,则旧数据模板生成的可视树将被丢弃,并新建一个可视树。
换句话说:询问如何在两个元素中查找元素毫无意义。
希望这可以为您清除所有麻烦,因为同一控件上的两个不同模板实例化了不同的可视树。
最后的建议:请仔细阅读MVVM,并停止尝试尽快访问DataTemplates中的元素。但是,如果您认为自己中有0.1%的人确实确实有理由访问模板中的元素,请写回您希望这样做的实际原因,也许我可以提供进一步的指导。
I've got 2 DataTemplates defined for a Listbox Control. 1 Template is for the UnSelected State and the other one is for the Selected State(showing more detail than the UnSelected State).
I followed the example here:
about how to access the Elements inside the DataTemplates from Code behind.
I get it right, but it only finds and returns an element of the UnSelected DataTemplate. But when i search for an element in the Selected DataTemplate i get a NullReferenceException.
What could i be doing wrong?
What you are doing wrong?
I would say what you are doing wrong is trying to access elements inside the DataTemplate from code-behind. Naughty, naughty!
All joking aside, 99.9% of the time I see someone trying to access an element inside a DataTemplate from code, it is because their application is poorly designed, with no (or few) bindings and no view model at all. Many beginners tend to store their data directly in UI elements rather than using a view model. I think it is because their minds have been corrupted by experience VB, WinForms, and ASP.NET where it was the "normal" way to do it. There are a thousand reasons to use a view model and bind your data instead of storing them in UI elements. Look up "model view view model" online for more details.
Now to answer your question:
Any given ListBoxItem can only have one DataTemplate at a time. This is because its ContentPresenter has only one ContentTemplate property, and this property cannot have two different values.
Because of this, the visual tree under a ListBoxItem will always be generated from one a specific template, not a combination of several templates. If you change the ItemTemplate of the ListBox or otherwise update ListBoxItem.ContentTemplate, the visual tree produced by the old template will be thrown away and a new one built.
Let me say that again: If you change data templates, the visual tree produced by the old data template will be thrown away and a new visual tree built.
You can have a hundred data templates defined and usable on a given ListBoxItem, but only one at a time can have a visual tree instantiated for it. And these are the only elements that actually exist as part of the visual tree. All other templates exist only as templates - there are no actual elements created for them.
To put it another way: It is meaningless to ask about how to find elements in two different visual trees instantiated by two different templates on the same control, because a single control cannot have two different templates active at the same time.
Hope this clears things up for you.
Final advice: Do read up on MVVM, and stop trying to access elements inside DataTemplates ASAP. However if you think you might be in that 0.1% who actually do have valid reasons to access elements inside templates, write back with your actual reason for wanting to do so and maybe I can provide further guidance.
这篇关于一个DataTemplate内的访问元素...如何为多个DataTemplate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!