To find controls in wpf, you need to use the VisualTreeHelper class, but this class does not have a method to find controls according to the name, so I searched the network and sorted out the following class, which I feel is very convenient to use.
Post it for your reference.
It's very simple to use, for example, find a text box called "myTextBox" in the template column in the datagrid, you can write it like this:
Note: When initializing the page, if the code to find the control is placed in the Loaded event, the control will not be found. It should be placed in the LayoutUpdated event, when the xaml is loaded and all the subcontrols can be retrieved.
However, the LayoutUpdated event will trigger as long as the page is updated, and if we want to achieve the effect of the Loaded event, we can set a flag to control the code to be executed only once when initializing, and I haven't thought of a better way to implement it, which is roughly as follows:
|