如何在WPF中使用可视树查找控件名称 [英] How to find the controls Name using visual tree in WPF

查看:391
本文介绍了如何在WPF中使用可视树查找控件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





有没有办法使用Visual树找到控件的实际名称。



即假设我在WPF窗口MovieWindow上的画布上有一个名为Run Movie的按钮。



在Visual树中使用Window控件,我可以使用下面的代码跟踪按钮。



Hi,

Is there any way to find the control''s actual name using Visual tree.

i.e let say I have a button called "Run Movie" on a canvas over WPF window "MovieWindow".

Using Window control in the Visual tree, I can trace button using below code.

dim myKid as DependencyObject = Nothing
for i as integer =0 to VisualTreeHelper.GetChildrenCount(MovieWindow) -1
  myKid = VisualTreeHelper.GetChild(MovieWindow,i)

  dim controlName as string = myKid.DependecyObjectType.Name
Next







在上面的代码中,controlName会将控件的名称设为Button。



如何使用可视树将按钮名称设置为运行电影。





先谢谢。

最好的问候,




In the above code controlName will get the name of the control as "Button".

How to get the button name as "Run Movie" using Visual tree.


Thanks in Advance.
Best Regards,

推荐答案

谢谢你们所有的努力。我发现下面的解决方案。



我们必须使用control.GetValue(Control.NameProperty)



例如,在下面的代码中,通过跟踪mainWindow即MovieWindow,我将获得Button。



要获取Button的名称,我应该这样做

Thanks guys for all your effort. I found out below solution.

We have to use control.GetValue(Control.NameProperty)

For Example, In the below code, by tracing mainWindow i.e MovieWindow, I will get Button.

To get the name of the Button, I should do
myKid.GetValue(Control.NameProperty)





这是通过使用dependecy属性GetValue method()。

(DependencyObject.GetValue(Control.nameProerty))





This is by using dependecy property GetValue method().
(DependencyObject.GetValue(Control.nameProerty))

dim myKid as DependencyObject = Nothing
for i as integer =0 to VisualTreeHelper.GetChildrenCount(MovieWindow) -1
  myKid = VisualTreeHelper.GetChild(MovieWindow,i)
 
  dim controlName as string = myKid.DependecyObjectType.Name
Next







最好的问候,



Vijay




Best Regards,

Vijay


这篇关于如何在WPF中使用可视树查找控件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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