WPF控件的附加属性



我仍在对WPF应用程序进行自动化测试。我需要通过名称访问属性来实现这一点。

目前我想知道WPF控件的附加属性。我尝试遍历按钮对象的所有属性,认为我也可以找到附加属性。但是我找不到他们。

我检查了using Snoop它列出了很多属性,比如"KeyboardNavigation "。AcceptsReturn"one_answers"ToolTipManager"。ToolTipKey",它应该是附加属性。

我用下面的代码创建了一个Button "object"的属性名称列表:

   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,结果列表为空。没有"IsAttached"检查,我得到了一个很好的属性列表,但没有任何预期的附加属性。

不反映列表附加属性的方式?


我想我现在更好地理解了附加属性的用法。然而,我不能真正解决我的问题。上面提到的本地枚举器只获取对象本地设置的属性,而不是对象可用的所有属性。

请让我解释一下我的意图:我只从附加属性的名称开始…我首先需要检查附加的属性是否存在(这可能意味着它是否已注册,对吧?)然后我想获取附加属性的值,这可能是本地设置的值为我的对象(如果一个设置)或默认值,否则。

目前我不知道如何检查是否存在附加属性。是否有一些函数提供所有可用附加属性的列表?我可以用它来验证给定的属性名并获取相应的属性对象。

附加属性不同于几种方式。最值得注意的是它们没有封装在CLR中属性(即标准的。net属性)。

详情请浏览http://joshsmithonwpf.wordpress.com/2007/06/22/overview-of-attached-properties-in-wpf/。

你可能想尝试使用GetLocalValueEnumerator来迭代你的属性http://msdn.microsoft.com/en-us/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很可能会改变。

最新更新