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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12747589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:40:52  来源:igfitidea点击:

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

c#wpf

提问by Fedor

I can get all UI controls on a Formbut how to find controls on a certain UserControl?

我可以得到一个上的所有 UI 控件,Form但是如何找到某个上的控件UserControl

回答by Aghilas Yakoub

You can use Linq operator OfTypeand 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

回答by TorbenJ

You may have a look at the FindName(string)method of UserControl

你可以看看FindName(string)方法UserControl

回答by Bill Tarbell

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 控件?