如何以编程方式读取附加依赖属性的值?
因此,我有一个带有 AutomationId 的按钮(由 Microsoft UI 自动化使用),如下所示:
<Button Name="myButton" AutomationId="myButtonAutomationID"
以编程方式,我在代码中有按钮 (myButton),如何获取附加到该按钮的“AutomationId”属性的值?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DependencyObject.GetValue
应该做的工作:DependencyObject.GetValue
should do the job:从根本上讲,就像使用任何其他 DependencyProperty 一样;对象上的普通属性作为(或应该)作为
DependencyObject.GetValue
和.SetValue
的简单包装器提供服务,因此您需要做的就是自己调用GetValue
并传入附加的DependencyProperty
的static readonly
实例:Fundamentally, just as you would with any other
DependencyProperty
; the ordinary properties on your object serve (or should serve) as simple wrappers aroundDependencyObject.GetValue
and.SetValue
, so all you need to do is callGetValue
yourself and pass in yourstatic readonly
instance of your attachedDependencyProperty
:作为依赖项属性的标准,此包装器方法将为您调用 DependencyObject.GetValue 并将值转换为正确的类型。
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.