开发者

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

开发者 https://www.devze.com 2023-03-19 04:49 出处:网络
So I have a Button with an AutomationId (used by Microsoft UI Automation) like so: <Button Name=\"myButton\" Automat开发者_如何学JAVAionId=\"myButtonAutomationID\"

So I have a Button with an AutomationId (used by Microsoft UI Automation) like so:

<Button Name="myButton" Automat开发者_如何学JAVAionId="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.

0

精彩评论

暂无评论...
验证码 换一张
取 消