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