views:

280

answers:

1

I have a custom control that has a Dependancy Property...it has a few but let us say Dragable is my problem. The property is a boolean and I want to execute a piece of code each time it changes...a toggle.

I have two options, both shown below

[Category("Modal Options")]
    public bool Dragable
    {
        get { return (bool)GetValue(DragableProperty); }
        set { SetValue(DragableProperty, value); toggleDragable(); }
    }

    // Using a DependencyProperty as the backing store for Dragable.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DragableProperty =
        DependencyProperty.Register("Dragable", typeof(bool), 
        typeof(PlussWindow), new PropertyMetadata(false));

    private void MakeDragable()
    {
        this.dragBehavior.Attach(this.LayoutRoot);
    }

    private void MakeUnDragable()
    {
        this.dragBehavior.Detach();
    }

    public virtual void toggleDragable()
    {
        if (this.Dragable)
        {
            MakeUnDragable();
        }
        else
        {
            MakeDragable();
        }
    }

or

[Category("Modal Options")]
    public bool Dragable
    {
        get { return (bool)GetValue(DragableProperty); }
        set { SetValue(DragableProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Dragable.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DragableProperty =
        DependencyProperty.Register("Dragable", typeof(bool), 
        typeof(PlussWindow), new PropertyMetadata(false, (o, e) => { (o as PlussWindow).toggleDragable(); } 
         ));

    private void MakeDragable()
    {
        this.dragBehavior.Attach(this.LayoutRoot);
    }

    private void MakeUnDragable()
    {
        this.dragBehavior.Detach();
    }

    public virtual void toggleDragable()
    {
        if (this.Dragable)
        {
            MakeUnDragable();
        }
        else
        {
            MakeDragable();
        }
    }

Each method results in a 'Object reference not set to an instance of an object'

I usually use binding to get over this problem, e.g Visibility or Text are easily done, but for custom functionality I need to enable this in code.

How do I do this, noting that the propertychanged method is static?

+1  A: 

Try this:

public bool Dragable
    {
        get { return (bool)GetValue(DragableProperty); }
        set { SetValue(DragableProperty, value); }
    }

    public static readonly DependencyProperty DragableProperty =
        DependencyProperty.Register("Dragable", typeof(bool), typeof(PlussWindow), new PropertyMetadata(false,PropertyChangedCallback(onDragableChange)));

    private static void onDragableChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        bool validate = (bool)e.NewValue;

        if (validate)
        {
            this.dragBehavior.Attach(this.LayoutRoot);
        }

        else
        {
            this.dragBehavior.Detach();
        }


    }
MarioEspinoza