如何为依赖属性创建公共事件?

发布于 2024-10-26 10:51:22 字数 538 浏览 4 评论 0原文

在下面的代码中,您可以看到我正在尝试执行的操作,但它不起作用。我想要一个可以附加到用户控件外部的事件,并在依赖属性发生更改时触发。

    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value"
        , typeof(Double)
        , typeof(ucSlider)
        , new PropertyMetadata(50d, new PropertyChangedCallback(OnValueChanged)));

    public Double Value
    {
        get { return (Double)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    public event PropertyChangedCallback OnValueChanged;

In the code below you can see what I'm trying to do, but it doesn't work. I want an event that I can attach to outside of my user control and fires whenever the dependency property changes.

    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value"
        , typeof(Double)
        , typeof(ucSlider)
        , new PropertyMetadata(50d, new PropertyChangedCallback(OnValueChanged)));

    public Double Value
    {
        get { return (Double)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    public event PropertyChangedCallback OnValueChanged;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2024-11-02 10:51:22

依赖属性是静态的,但您的事件与类的实例相关。所以你需要一个介于静态属性和实例事件之间的中间方法。

在此示例中,我将静态方法 OnValuePropertyChanged 作为回调参数传递,并在其中引发事件:

public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register("Value"
    , typeof(Double)
    , typeof(ucSlider)
    , new PropertyMetadata(50d, new PropertyChangedCallback(OnValuePropertyChanged)));

public Double Value
{
    get { return (Double)GetValue(ValueProperty); }
    set { SetValue(ValueProperty, value); }
}

public static void OnValuePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    var sl = sender as ucSlider;
    if (sl != null)
        sl.RaiseValueChangedEvent(e);
}

private void RaiseValueChangedEvent(DependencyPropertyChangedEventArgs e)
{
    if(this.OnValueChanged != null)
        this.OnValueChanged(this, e);
}

public event PropertyChangedCallback OnValueChanged;

Dependency properties are static, but your event is related with the instance of the class. So you need an intermediate method between the static property and the event of instance.

In this example I pass the static method OnValuePropertyChanged as a callback parameter and inside it I raise the event:

public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register("Value"
    , typeof(Double)
    , typeof(ucSlider)
    , new PropertyMetadata(50d, new PropertyChangedCallback(OnValuePropertyChanged)));

public Double Value
{
    get { return (Double)GetValue(ValueProperty); }
    set { SetValue(ValueProperty, value); }
}

public static void OnValuePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    var sl = sender as ucSlider;
    if (sl != null)
        sl.RaiseValueChangedEvent(e);
}

private void RaiseValueChangedEvent(DependencyPropertyChangedEventArgs e)
{
    if(this.OnValueChanged != null)
        this.OnValueChanged(this, e);
}

public event PropertyChangedCallback OnValueChanged;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文