[英]Attached properties of WPF controls
我仍在對WPF應用程序進行自動化測試。 我需要按名稱訪問屬性以實現此目的。
當前,我想知道WPF控件的附加屬性。 我試圖遍歷Button對象的所有屬性,以為我也可以找到附加的屬性。 但是我找不到他們。
因此,我使用Snoop進行了檢查,它列出了很多屬性,例如“ KeyboardNavigation.AcceptsReturn”和“ ToolTipManager.ToolTipKey”,應該附加屬性AFAIK。
我使用以下代碼創建了按鈕“對象”(附加)屬性的名稱列表:
Type^ type = object->GetType();
while (type)
{
array<FieldInfo^>^ fi = type->GetFields(BindingFlags::DeclaredOnly | BindingFlags::Static | BindingFlags::Public);
for (int i=0; i<fi->Length; i++)
{
DependencyProperty^ dp = dynamic_cast<DependencyProperty^>(fi[i]->GetValue(object));
if (dp)
{
DependencyPropertyDescriptor^ dpd = DependencyPropertyDescriptor::FromProperty(dp, type);
if (dpd->IsAttached)
{
propertyNames->Add(fi[i]->Name);
}
}
}
type = type->BaseType;
}
但是,IsAttached始終為false,結果lsit為空。 沒有“ IsAttached”檢查,我會得到不錯的屬性列表,但沒有任何預期的附加屬性。
不反映那樣列出附加屬性嗎?
我想我現在更好地了解了附加屬性的用法。 但是,我實際上無法解決我的問題。 提到的本地枚舉器僅獲取在本地對對象設置的屬性,而不是可用於該對象的所有屬性。
請讓我解釋一下我的打算:我僅從附加屬性的名稱開始...我首先需要檢查該附加屬性是否存在(這可能意味着它是否已注冊,對嗎?)。 然后,我想獲取附加屬性的值,該值可以是對象的本地設置值(如果已設置),否則可以是默認值。
目前,我不知道如何檢查附加屬性是否存在。 也許有一些功能可以提供所有可用附加屬性的列表? 我可以使用它來驗證給定的屬性名稱並獲取相應的屬性對象。
附加屬性在某些方面與常規依賴項屬性不同。 最值得注意的是,它們沒有包裝在CLR屬性(即標准.NET屬性)中。
有關更多詳細信息,請訪問http://joshsmithonwpf.wordpress.com/2007/06/22/overview-of-attached-properties-in-wpf/ 。
您可能想要嘗試使用GetLocalValueEnumerator來遍歷屬性http://msdn.microsoft.com/zh-cn/library/system.windows.dependencyobject.getlocalvalueenumerator.aspx
抱歉,工作一直讓我忙。 您可以這樣做:
假設xaml:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button x:Name="btn" Grid.Column="1"/>
</Grid>
以下代碼應為您提供一些選擇:
//do this if you can:
btn.GetValue(Grid.ColumnProperty);
//Otherwise,
//gets all the dependency properties in the app domain
Type depType = typeof(DependencyProperty) ;
FieldInfo info = depType.GetField("PropertyFromName", BindingFlags.NonPublic | BindingFlags.Static);
Hashtable AllDependencyProperties = info.GetValue(null) as Hashtable;
//Index the hashtable of all dependency properties using a FromNameKey type
Type FromNameKeyType = depType.Assembly.GetType("System.Windows.DependencyProperty+FromNameKey");
ConstructorInfo ctor = FromNameKeyType.GetConstructor(new Type[] { typeof(String), typeof(Type) });
var NameKey = ctor.Invoke(new object[] { "Column", typeof(Grid) });
//index the hashtable to get the Dependency property
DependencyProperty dp = AllDependencyProperties[NameKey] as DependencyProperty;
//use the dp to get the value
btn.GetValue(dp);
//Or, without indexing a list of all dependency properties
//get a dependency property by name
System.Reflection.MethodInfo FromNameMethod = depType.GetMethod("FromName",System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic );
var ret = FromNameMethod.Invoke(null, new object[] { "Column", typeof(Grid) });
//use it to get a value from an object
btn.GetValue((ret as DependencyProperty));
警告:這使用的是私有成員和類,MS可能會在將來的某些版本中進行更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.