Pergunta

I have custom attached property in shared lib:

public class Freezable
{
    public static bool GetIsFrozen(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsFrozenProperty);
    }

    public static void SetIsFrozen(DependencyObject obj, bool value)
    {
        obj.SetValue(IsFrozenProperty, value);
    }

    public static readonly DependencyProperty IsFrozenProperty =
        DependencyProperty.RegisterAttached("IsFrozen", typeof(bool), typeof(UIElement), new UIPropertyMetadata(false));
}

This property was tested in code and value was successfully stored.

Freezable.SetIsFrozen(control, value);
var correctValue =Freezable.GetIsFrozen(control);

But then i am trying to get my property via Reflection. And I have exception that property cannot be find (getMethod is null and setMethod is null)!

 public void FindAttachedProperty(Type elementType, string propertyName)
    {
        MethodInfo getMethod = elementType.GetMethod("Get" + propertyName, BindingFlags.Public | BindingFlags.Static);
        MethodInfo setMethod = elementType.GetMethod("Set" + propertyName, BindingFlags.Public | BindingFlags.Static);
    }

Canvas.Left, Canvas.Right working well and without any problems. What i am doing wrong and why i cannot get my attached property!?

Foi útil?

Solução

Your code works fine for me. My best guess is that when you are calling FindAttachedProperty method you are giving the System.Windows.Freezable as elementType instead of your own implementation from library.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top