簡體   English   中英

如何以編程方式讀取附加依賴屬性的值?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM