This example lets you find controls by entering their names. When you enter a control’s name and a color and click Apply, the program changes the selected control’s background color.
You can use XAML code to give names to the controls in your application and the C# code behind can refer to the controls by those names. If you want to find a control by its name at run time, however, you have a bit of work to do.
To use the program, you need to know the names of its controls. The program uses the following XAML code. The control names are highlighted in blue so they are easy to see.
The controls in a WPF application form a hierarchy. To find controls by name, you can start at the top of the hierarchy and recursively work your way down through the controls until you find the one that you want. Unfortunately WPF controls don’t provide a simple Children collection that you can examine. Instead you need to use the VisualTreeHelper class’s GetChildrenCount method to see how many children a control has. You then need to us the same class’s GetChild method to get the children.
The following FindDescendant method searches a control hierarchy for a control with the given name.
The method first converts the parent control into a FrameworkElement. If the result is not null (indicating that the parent actually inherits from FrameworkElement) and the element has the desired name, then the method simply returns it.
Notice that the code compares the names as they are. That means, for example, that you cannot search for a control named TextBox1 by entering the name textbox1. You can modify the code to make it case-insensitive if you like.
If the parent control does not have the target name, the program uses the VisualTreeHelper class to see how many children the parent has. It then loops through the children and recursively calls FindDescendant for each.
If one of the recursive calls returns a non-null descendant with the correct name, the method returns it. If none of the recursive calls finds the desired control, the method return null to tell method calls farther up the call stack that the target control is not present in this part of the control hierarchy.
When you click the Apply button, the following code executes.
This code is actually error handling in case you enter an invalid control name or color.
The code first gets the control name that you entered and calls FindDescendant to find it. The program uses its main window as the top of the control hierarchy to search. If you look back at the way the FindDescendant method works, you’ll see that it starts by checking the parent control to see if it has the target name. That means you can enter MainWindow as a control name and the program will find the top-level window.
If the FindDescendant method returns null, the code displays an error message and returns.
If the method found a control, the program gets the color name that you entered. It then uses the ColorConverter class’s ConvertFromString method to convert that name into a Color. Note that the ConvertFromString method is case-insensitive so LightBlue, lightblue, and LIGHTblue will all work. The color must be a single word and spelling is important, however, so LiteBlue and Light Blue will not work.
If the ConvertFromString method throws an exception, the code displays an error message and returns.
If the program successfully obtains the control and color, it tries to set the control’s background color. To do that, the program determines whether the object is a Panel or Control. If the object is of one of those types, the code converts it into that type and sets its Background property to an appropriate brush.
This is all a bit weird because the object that the program finds is actually a DependencyObject. Unfortunately that class does not have a Background property. Descendant classes such as Panel, Control, TextBlock, and others have the property.
The Panel class is an ancestor of classes such as Grid and StackPanel, which are used by the program. Similarly the Control class is an ancestor of the Label, Button, and TextBox classes used by the program.
The program looks for Panel and Control objects because it uses them. You might need to look for other kinds of controls, such as TextBlock if your program uses them.
Finding controls recursively isn’t new. You can use a similar technique to find controls in Windows Forms applications, too. WPF makes it all a bit more awkward by making you use the VisualTreeHelper class and by placing the Background property in several different classes, but the general approach is similar.
Download the example to see additional details and to experiment with the program.