开发者

Access Elements inside a DataTemplate... How to for more than 1 DataTemplate?

开发者 https://www.devze.com 2023-01-03 01:06 出处:网络
I\'ve got 2 DataTemplates defined for a Listbox Control. 1 Template is for the UnSele开发者_如何学运维cted State and the other one is for the Selected State(showing more detail than the UnSelected Sta

I've got 2 DataTemplates defined for a Listbox Control. 1 Template is for the UnSele开发者_如何学运维cted State and the other one is for the Selected State(showing more detail than the UnSelected State).

I followed the example here:

Link

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?


Setting keyboard focus might be one reason you need to access the datatemplate elements. MVVM will not solve that issue and the FocusManager doesn't set keyboard focus.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消