views:

125

answers:

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:

http://blogs.msdn.com/b/wpfsdk/archive/2007/04/16/how-do-i-programmatically-interact-with-template-generated-elements-part-ii.aspx

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?

A: 

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.

Ray Burns
Hi Ray, Thanks for the feedback. I did however managed to sort it out. It didnt work because i was trying to access it through the ListBox_Selection changed event handler, and for some reason it passes a NULL obj to the FindVisualChild Method. I moved the code to the Event Handler i'm supposed to use it in, changed the "IsSyncronisedWithCurrentItem" to True, and it worked.However, you are right. I'm am a beginner Developer, starting with VB.NET, stumbled across WPF and fell in love with it. Still so much for me to learn and understand.
GaaTY
I understand where you are: I was a WPF beginner once. Stick with it - WPF has a comparatively long learning curve, but what you can do with it when you're there is simply amazing!
Ray Burns