Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

VB
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



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,
Posted
Updated 2-Dec-12 19:42pm
v2

1 solution

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
VB
myKid.GetValue(Control.NameProperty)


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



Best Regards,

Vijay
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900