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(); }
}
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); }
}
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?