Shown below are two useful methods that works on a VisualTreeHelper class and makes it easy to access elements that are otherwise not easy to access. I would show you concrete examples to use it later, but for now here is the code for the two methods.
1: public List<T> GetChildrenOfType<T>(DependencyObject parent) where T : DependencyObject
2: {3: List<T> list = new List<T>();
4: int childCount = VisualTreeHelper.GetChildrenCount(parent);
5: for (int i = 0; i < childCount; i++)
6: { 7: DependencyObject child = VisualTreeHelper.GetChild(parent, i);8: //get the current child
9: if (child is T) list.Add((T)child);
10: //if it is of type that you are looking for, then add it to the list
11: list.AddRange(GetChildrenOfType<T>(child)); // on that get a list of children that it has.
12: } 13: return list;
1: public T GetChild<T>(DependencyObject parent) where T : DependencyObject
2: {3: //if(parent is Label) return parent;
4: int childCount = VisualTreeHelper.GetChildrenCount(parent);
5: for (int i = 0; i < childCount; i++)
6: { 7: DependencyObject child = VisualTreeHelper.GetChild(parent, i);8: if (child is T) return (T)child;
9: T childLabel = GetChild<T>(child);10: if (childLabel != null) return childLabel;
11: } return default(T);
12: }1: private void ToggleButton_Click(object sender, RoutedEventArgs e)
2: { 3: ToggleExpandCollapse(tvQueries); //start toggling with the parent.
4: } 5: 6: private void ToggleExpandCollapse(DependencyObject dO)
7: { 8: foreach (TreeViewItem ti in GetChildrenOfType<TreeViewItem>(dO))
9: { 10: ti.IsExpanded = !ti.IsExpanded; //toggle expanded property.
11: // update the layout such that Visual Tree gets updated
12: tvQueries.UpdateLayout(); 13: //now repeat the toggling on the current TreeViewItem
14: ToggleExpandCollapse(ti); 15: } 16: } 1: <Style TargetType="TreeViewItem">
2: <Setter Property="IsExpanded" Value="True" />
3: </Style>