Issue with changing Silverlight Dependancy properties

I have a user control that has the Dependancy Property ... it has a few, but let's say Dragable is my problem. The property is logical, and I want to execute a piece of code every time it changes ... the switch.

I have two options as 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 leads to the fact that "The reference to the object is not installed in the instance of the object

I usually use binding to solve this problem, for example, visibility or text is easy to execute, but for custom functions I need to include this in the code.

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

+3
1

:

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, new PropertyChangedCallback(onDragableChange)));

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

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

        else
        {
            win.dragBehavior.Detach();
        }


    }
+3

Source: https://habr.com/ru/post/1731012/


All Articles