0

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);

}
Sam
  • 1,509
  • 3
  • 19
  • 28
uriya
  • 1

1 Answers1

0

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

Timores
  • 14,439
  • 3
  • 46
  • 46
  • 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 Apr 29 '10 at 12:51