title: Attached properties of WPF controls
body: <p>I am still working on automated testing of WPF apps. I need to access properties by name to achieve this.</p>
  
  <p>Currently I am wondering about attached properties of WPF controls. I tried to iterate through all properties of a Button object thinking I could find attached properties too. But I couldn't find them.</p>
  
  <p>So I checked using Snoop and it lists a lot of properties like "KeyboardNavigation.AcceptsReturn" and "ToolTipManager.ToolTipKey" which should be attached properties AFAIK.</p>
  
  <p>I used the following code to create a list of names of (attached) properties of Button "object":</p>
  
  <pre><code>   Type^ type = object-&gt;GetType();
     while (type)
     {
        array&lt;FieldInfo^&gt;^ fi = type-&gt;GetFields(BindingFlags::DeclaredOnly | BindingFlags::Static | BindingFlags::Public);
        for (int i=0; i&lt;fi-&gt;Length; i++)
        {
           DependencyProperty^ dp = dynamic_cast&lt;DependencyProperty^&gt;(fi[i]-&gt;GetValue(object));
           if (dp)
           {
              DependencyPropertyDescriptor^ dpd = DependencyPropertyDescriptor::FromProperty(dp, type);
              if (dpd-&gt;IsAttached)
              {
                 propertyNames-&gt;Add(fi[i]-&gt;Name);
              }
           }
        }
  
        type = type-&gt;BaseType;
     }
  </code></pre>
  
  <p>However IsAttached is always false and the resulting lsit is empty. Without the "IsAttached" check I get a nice list of properties but without any of the expected attached properties.</p>
  
  <p>Doesn't reflect list the attached properties that way?</p>
  
  <hr>
  
  <p>I think I now better understand usage of attached properties. However I could not actually solve my problem.
  The mentioned local enumerator only gets the properties that are set on the object locally, not <em>all</em> the properties available for the object.</p>
  
  <p>Please let me explain what I intend: I am starting with the <em>name</em> of an attached property only... I first need to check if that attached property exists (which probably means if it is registered, right?). Then I want to fetch the attached property's value which may be the locally set value for my object (if one was set) or the default value otherwise.</p>
  
  <p>Currently I don't know how to check if an attached property exists. Is there maybe some function providing a list of <em>all</em> available attached properties? I could use it for validating a given property name and to fetch the corresponding property object.</p>