3

I recently had an issue with databinding to the Visibility property of a DataGridTextColumn. The confusion arose because this property is a dependecy property in WPF but not in Silverlight.

I don't think that the MSDN documentation makes this very clear. The following is the only related text for WPF.

"For information about what can influence the value, see DependencyProperty."

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcolumn.visibility(v=VS.100).aspx

Scott Munro
  • 13,369
  • 3
  • 74
  • 80

4 Answers4

4

Dependency properties have a corresponding static field on the class they are defined in. Have a look at the fields section of the DataGridTextColumn class.

Jens
  • 25,229
  • 9
  • 75
  • 117
2

In most cases you can detect whether a property Foo is a DP by checking if there is a static field named FooProperty of type DependencyProperty. However, this is only a convention. There is no guarantee that all dependency properties will follow this pattern.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
1

Already answered, I know. IE. The "Text" property in a "TextBlock" is a dependency property you can tell because Intellisense shows the static filed like this:

TextBlock.TextProperty

NestorArturo
  • 2,476
  • 1
  • 18
  • 21
0

Here's an extension method that evaluates a property's value and also returns the corresponding DependencyProperty:

    public static bool TryEvaluateProperty(this FrameworkElement fe, string property, out object value, out DependencyProperty dp)
    {
        value = default;
        dp = default;

        var feType = fe.GetType();
        var propertyInfo = feType.GetProperty(property, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);

        if (propertyInfo == null) return false;

        value = propertyInfo.GetValue(fe);

        var dpName = property + "Property"; //ASSUMPTION: Definition uses DependencyProperty naming convention

        while (feType != null)
        {
            var dpField = feType.GetField(dpName, BindingFlags.Static | BindingFlags.Public);

            if (dpField != null)
            {
                dp = dpField.GetValue(null) as DependencyProperty; //use obj==null for static field
                break;
            }

            feType = feType.BaseType;
        }

        return true;
    }
mike
  • 1,627
  • 1
  • 14
  • 37