![](/img/trans.png)
[英]How do I listen to changes in Dependency properties from within an attached dependency property?
[英]How do I programmatically read the value of an attached dependency property?
所以我有一個帶有 AutomationId 的按鈕(由 Microsoft UI 自動化使用),如下所示:
<Button Name="myButton" AutomationId="myButtonAutomationID"
以編程方式,我在代碼中有按鈕 (myButton),如何獲取附加到該按鈕的“AutomationId”屬性的值?
DependencyObject.GetValue
應該做的工作:
string automationId =
(string)myButton.GetValue(AutomationProperties.AutomationIdProperty);
從根本上說,就像使用任何其他DependencyProperty
一樣; object 上的普通屬性服務(或應該服務)作為DependencyObject.GetValue
和.SetValue
的簡單包裝器,所以你需要做的就是自己調用GetValue
並傳入你的static readonly
實例附加的DependencyProperty
:
var value = myButton.GetValue(yourDependencyProperty);
var automationId = AutomationProperties.GetAutomationId(myButton);
作為依賴屬性的標准,此包裝器方法將為您調用DependencyObject.GetValue
,並將值轉換為正確的類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.