Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically read the value of an attached dependency property?

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?

like image 670
Allen Firth Avatar asked Jul 11 '11 14:07

Allen Firth


3 Answers

DependencyObject.GetValue should do the job:

string automationId = 
    (string)myButton.GetValue(AutomationProperties.AutomationIdProperty);
like image 115
Florian Greinacher Avatar answered Nov 12 '22 03:11

Florian Greinacher


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);
like image 42
Adam Robinson Avatar answered Nov 12 '22 03:11

Adam Robinson


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.

like image 29
Kent Boogaart Avatar answered Nov 12 '22 04:11

Kent Boogaart