如何确定 DependencyProperty 的更改内容

发布于 2024-10-04 19:31:20 字数 1071 浏览 0 评论 0原文

我有两个对象绑定到相同的 dependencyProperty(在 Silverlight 中)。 有没有办法确定这两个对象中哪一个改变了属性? 我想根据这些信息采取不同的行动。

不幸的是,我无法附加两个不同的事件处理程序(因为它是一个 dependencyProperty)

   public int StartTime
    {
        get { return (int)GetValue(StartTimeProperty); }
        set { SetValue(StartTimeProperty, value); }
    }
    public static readonly DependencyProperty StartTimeProperty =
        DependencyProperty.Register("StartTime", typeof(int), typeof(Step),
        new PropertyMetadata(-1, new PropertyChangedCallback(OnStartTimeChanged)));

    private static void OnStartTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((Step)d).OnStartTimeChanged(e);
    }

    protected virtual void OnStartTimeChanged(DependencyPropertyChangedEventArgs e)
    {
        //if set from obj1 -> do something
        //if set from obj2 -> do something else
    }

在此示例中,我将从不同的对象设置 StartTime 属性,并且我想知道其中哪个对象更改了该属性。

谢谢

I've got two objects bound to the same dependencyProperty (in Silverlight).
Is there a way to determine which of these two objects changed the property?
I want to take different actions based on that information.

Unfortunately, I cannot attach two different eventHandlers (because it's a dependencyProperty)

   public int StartTime
    {
        get { return (int)GetValue(StartTimeProperty); }
        set { SetValue(StartTimeProperty, value); }
    }
    public static readonly DependencyProperty StartTimeProperty =
        DependencyProperty.Register("StartTime", typeof(int), typeof(Step),
        new PropertyMetadata(-1, new PropertyChangedCallback(OnStartTimeChanged)));

    private static void OnStartTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((Step)d).OnStartTimeChanged(e);
    }

    protected virtual void OnStartTimeChanged(DependencyPropertyChangedEventArgs e)
    {
        //if set from obj1 -> do something
        //if set from obj2 -> do something else
    }

In this example I would be setting StartTime property from different objects and I want to know which of these object changed the property.

Thanks

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

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

发布评论

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

评论(3

梦断已成空 2024-10-11 19:31:20

您可以:

  • 查看事件处理程序中的发送者
  • 将两个控件附加到不同的事件处理程序

You can either:

  • look at the sender in the event handler
  • attach both controls to different event handlers
海风掠过北极光 2024-10-11 19:31:20

我最终捕获了控件上的 mouseDown 事件,因此我知道 dependencyProperty 的值已由用户界面更改。这不是最干净的解决方案,但它有效。

非常感谢您的所有建议。

I have ended up catching a mouseDown event on the control so I knew the value of a dependencyProperty was changed by the user interface. It is not the cleanest solution but it worked.

Many thanks for all your suggestions.

情痴 2024-10-11 19:31:20
var descriptor = DependencyPropertyDescriptor.FromProperty(YourType.StartTimeProperty , tpeof(YourType));
descriptor.AddValueChanged(obj1, OnStartTimeChanged1);
descriptor.AddValueChanged(obj2, OnStartTimeChanged2);
var descriptor = DependencyPropertyDescriptor.FromProperty(YourType.StartTimeProperty , tpeof(YourType));
descriptor.AddValueChanged(obj1, OnStartTimeChanged1);
descriptor.AddValueChanged(obj2, OnStartTimeChanged2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文