views:

115

answers:

1

I'm trying to find the TextBlock that is inside the control template of a comboBox. using VisualTreeHelpar.GetChildrenCount is working only if the comboBox is declared in XAML.In that case GetChildrenCount returns 1 and a recursive search is possible.

However, if I declare the combo as a member of the Window class using code, allocated and setting it to its place, the function GetChildrenCount return 0. When I run snoop in this scenario It shows the combo children hierarchy. I want to be able to search the comboBox just as snoop does. Any help would be appreciated. code:

ComboBox mCombo = null;

private void Windows_Loaded(object sender, RoutedEventArgs e)

{

mCombo = new ComboBox;

mGrid.Children.Add(mCombo);

Grid.SetRow(mCombo,0);

int count = VisualTreeHelpar.GetChildrenCount(mCombo);

}

A: 

Call the ApplyTemplate method of ComboBox. Then, you should be able to find what you need.

Timores
thanks' that help up to a certain extent, and I'm able to go deep throug the visual tree. Still when I get to ContentPresenter element with in the ComboBox, snoop show it has 1 child which is a TextBlock, whereas VisualTreeHelper.GetChildrenCount return 0 ,even when I call explicitly to ApplyTemplate on ContentPresenter itself befor calling VisualTreeHelper.GetChildrenCount.So again, how can I get To this TextBlock?
uriya