How to get children of a WPF container by type?
This extension method will search recursively for child elements of the desired type:
public static T GetChildOfType<T>(this DependencyObject depObj)
where T : DependencyObject
{
if (depObj == null) return null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
var result = (child as T) ?? GetChildOfType<T>(child);
if (result != null) return result;
}
return null;
}
So using that you can ask for MyContainer.GetChildOfType<ComboBox>()
.
Children is a collection of UIElements. So you need to iterate over the items and determine for each item whether it is of the required type. Fortunately, there is already a Linq method for exactly this, namely Enumerable.OfType<T>
, which you can conveniently call using Extension Method syntax:
var comboBoxes = this.MyContainer.Children.OfType<ComboBox>();
This method filters the collection based on their type and returns, in your case, only the elements of type ComboBox
.
If you only want the first ComboBox (as your variable name might suggest), you can just append a call to FirstOrDefault()
to the query:
var myComboBox = this.MyContainer.Children.OfType<ComboBox>().FirstOrDefault();