This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17320|Reply: 0

[WPF] Extension class for lookup controls in WPF

[Copy link]
Posted on 5/30/2016 6:04:34 PM | | |
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:






Previous:WPF looks for CheckBox controls in the DataGrid by reflection
Next:Recommend a VS highlighting plugin
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com