简体   繁体   English

如何在UserControl组件上查找WPF中的所有控件

[英]How to find all the controls in WPF on a UserControl component

我可以获取Form上的所有UI控件,但如何在某个UserControl上找到控件?

You can use Linq operator OfType and Controls property 您可以使用Linq operator OfTypeControls property

var controls = YourForm
               .YourUserControl
               .Controls.OfType<TextBox>();
foreach(var control in controls)
{
    ....
}

Link : http://msdn.microsoft.com/fr-fr/library/vstudio/bb360913.aspx 链接: http//msdn.microsoft.com/fr-fr/library/vstudio/bb360913.aspx

您可以查看UserControlFindName(字符串)方法

If you only want to look in the immediate object you can do FindName : 如果您只想查看直接对象,可以执行FindName

object foundControl = someParentControl.FindName("nameOfChild");

or if you want recursive ways then you can look at this post here: How can I find WPF controls by name or type? 或者如果你想要递归方式,那么你可以在这里查看这篇文章: 如何通过名称或类型找到WPF控件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM