Get control from datatemplate in longlistselector

note: what you’re about to read is a long blah about some computer programming trial&error and possibly filled with newbie mistakes.

So I was working on the stocktwits app for wp8, and I ran into this problem:

I defined a datatemplate in app.xaml, and used it as itemtemplate in a longlistselector in another xaml file. The template contains an image and a couple of textblocks. What I want to achieve is that when I tap the image it navigates to page1, when I tap anything else it goes to page 2.

It would be easy if I define the datatemplate in the same file, but since I’m sharing the template among several different pages I can’t do this. And it’s a bad idea to put event handlers in app.xaml. The main problem I have is that I can’t assign an event handler to the control since I don’t have direct access to it. So…what should I do?

I ended up using a pretty brutal way to solve it. Using this function, I managed to iterate/search for every control defined in that datatemplate, and look up based on control type. It works for now, but once I put more than one control of the same type I can see myself getting into trouble. I also saw some code that does the lookup via name, I may use that to enhance the function(maybe overload it?)

Posted in Tech

Leave a Reply

Your email address will not be published. Required fields are marked *

*