views:

22

answers:

1

I have a datagrid and I'm writing a method to search through it to find some text. Practically all of my columns use a DataTemplateSelector, so in my search, I need to be able to take a DataTemplate, apply it to a ContentControl, and then find a TextBlock to get the text to see if it matches my search criteria. I'm trying the following but it's not seeming to produce any results. I also tried a FindName("layoutRoot" control) but that came back as null as well.

var control = new ContentControl();
control.ContentTemplate = dataTemplate;
control.Content = item;

var txtBox = control.FindChildren<TextBlock>();
A: 

Have you tried VisualTreeHelper's GetChild()/GetChildrenCount() to enumerate all children?

Additionally, I believe you can set Initialized handler from XAML on a target control in a DataTemplate and from there subscribe to Loaded event (if needed).

wpfwannabe