如何按名称在网格中找到子控件? [英] how can i find a child control in a grid by name?

查看:90
本文介绍了如何按名称在网格中找到子控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在网格中按名称查找子控件?
我有一个网格,并在其中添加了一些动态控制

我想按名称在网格中找到一个控件

how can i find a child control in a grid by name?
i have a grid and add some dinamic control in it

i want to find a control in my grid by name

how can do this?

推荐答案

您可以使用< code> System.Windows.Controls.Primitives</code>中的GetVisualDescendants.命名空间.
< pre>
IEnumerable< type> o =(((DependencyObject)_myGrid).GetVisualDescendants().OfType< type>();

foreach(在o中输入objChild类型)
{
如果(objChild.Name =="name")
{
//做点事
}
} pre>


这里的Type是您要查找的控件的类型.
You could use the GetVisualDescendants from the <code>System.Windows.Controls.Primitives</code> namespace.
<pre>
IEnumerable<type> o = ((DependencyObject)_myGrid).GetVisualDescendants().OfType<type>();

foreach(Type objChild in o)
{
if (objChild.Name == "name")
{
//Do something
}
}pre>


Here Type is the type of control you are looking for.


我尝试发生此错误:

Error 1 ''System.Windows.DependencyObject'' does not contain a definition for ''GetVisualDescendants'' and no extension method ''GetVisualDescendants'' accepting a first argument of type ''System.Windows.DependencyObject'' could be found (are you missing a using directive or an assembly reference?)
i try it this error Occurred:

Error 1 ''System.Windows.DependencyObject'' does not contain a definition for ''GetVisualDescendants'' and no extension method ''GetVisualDescendants'' accepting a first argument of type ''System.Windows.DependencyObject'' could be found (are you missing a using directive or an assembly reference?)


这篇关于如何按名称在网格中找到子控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆