So I have a Button with an AutomationId (used by Microsoft UI Automation) like so:
<Button Name="myButton" AutomationId="myButtonAutomationID"
Programmatically, I have the button (myButton) in code, how do I get the value of the 'AutomationId' property attached to that button?
DependencyObject.GetValue
should do the job:
string automationId =
(string)myButton.GetValue(AutomationProperties.AutomationIdProperty);
Fundamentally, just as you would with any other DependencyProperty
; the ordinary properties on your object serve (or should serve) as simple wrappers around DependencyObject.GetValue
and .SetValue
, so all you need to do is call GetValue
yourself and pass in your static readonly
instance of your attached DependencyProperty
:
var value = myButton.GetValue(yourDependencyProperty);
var automationId = AutomationProperties.GetAutomationId(myButton);
As is standard for dependency properties, this wrapper method will do the job of calling DependencyObject.GetValue
for you, and casting the value to the correct type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With