gpt4 book ai didi

Silverlight 依赖属性更改事件问题

转载 作者:行者123 更新时间:2023-12-02 00:42:25 26 4
gpt4 key购买 nike

我有一个具有依赖属性的自定义控件...它有几个,但让我们说可拖动是我的问题。该属性是一个 bool 值,每次更改时我都想执行一段代码...一个切换。

我有两个选项,都显示在下面

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

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

每个方法都会导致“对象引用未设置为对象的实例”

我通常使用绑定(bind)来解决这个问题,例如,可见性或文本很容易完成,但对于自定义功能,我需要在代码中启用它。

我该怎么做,注意到 propertychanged 方法是静态的?

最佳答案

试试这个:

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


}

关于Silverlight 依赖属性更改事件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2184201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com