How to programmatically read the value of an attached dependency property?

So, I have a button with AutomationId (used by Microsoft UI Automation):

<Button Name="myButton" AutomationId="myButtonAutomationID" 

Programmatically, I have a button (myButton) in the code, how do I get the value of the AutomationId property attached to this button?

+6
source share
3 answers

DependencyObject.GetValue should complete the task:

 string automationId = (string)myButton.GetValue(AutomationProperties.AutomationIdProperty); 
+7
source

In principle, the same as with any other DependencyProperty ; the usual properties on your object serve (or should serve) like simple wrappers around DependencyObject.GetValue and .SetValue , so all you have to do is call GetValue yourself and pass your static readonly instance of your attached DependencyProperty

 var value = myButton.GetValue(yourDependencyProperty); 
+1
source
 var automationId = AutomationProperties.GetAutomationId(myButton); 

As a standard for dependency properties, this wrapper method will do the task of calling DependencyObject.GetValue for you and injecting the value into the correct type.

0
source

Source: https://habr.com/ru/post/892503/


All Articles