Find Control of a Type in WPF
I had previously written a piece of Code to retrieve a control of a single type in ASP.net, now working on WPF I had to do basically the same thing, and VisualTreeHelper came to the rescue.
In this process I discovered that if the content of the user control/window is a scroll panel you have to reference the scroll panel. so without further blabber , here is the code. (Please Note that this also works with controls inside a groupbox)
public static class VisualEnumerable { /// <summary> /// Gets the Visual Tree filtered by Type for a DependencyObject with that DependencyObject as the root. /// </summary> public static IEnumerable<t> GetVisualOfType</t><t>(this DependencyObject element) { var temp = GetVisualTree(element).ToList(); return temp.Where(t => t.GetType() == typeof(T)).Cast</t><t>(); } /// <summary> /// Gets the Visual Tree for a DependencyObject with that DependencyObject as the root. /// </summary> public static IEnumerable<dependencyobject> GetVisualTree(this DependencyObject element) { int childrenCount = VisualTreeHelper.GetChildrenCount(element); for (int i = 0; i < childrenCount; i++) { var visualChild = VisualTreeHelper.GetChild(element, i); if (visualChild != null) yield return visualChild; if (visualChild is GroupBox) { var gb = visualChild as GroupBox; object gpChild = gb.Content; yield return visualChild; visualChild = (DependencyObject)gpChild; } foreach (var visualChildren in GetVisualTree(visualChild)) { yield return visualChildren; } } } }
To Use this you can simply do
var textboxes = this.MainPanel.GetVisualOfType<textbox>() </textbox>