我如何知道依赖属性的绑定已更改?

发布于 2024-11-25 08:59:09 字数 1245 浏览 1 评论 0原文

正如这个问题中提到的,至少有两种方法可以通知绑定到依赖项属性的值已更改:

  1. DependencyPropertyDescriptor.AddValueChanged
  2. DependencyProperty.OverrideMetadata 在具有我自己的 PropertyChangedCallback 的派生类上。

这一切都工作正常,但仅当在属性上设置实际绑定时才需要通知我,而不是每次值更改时。有没有办法为此或我需要监听的事件注册回调?

我在 MSDN 中查看了类 DependencyPropertyDependencyObjectBindingOperations 和 依赖属性描述符

As mentioned in this question, there are at least two ways by which I can be notified that the value bound to a dependency property has changed:

  1. DependencyPropertyDescriptor.AddValueChanged
  2. DependencyProperty.OverrideMetadata on a derived class with my own PropertyChangedCallback.

This is all working fine but I need to be notified only when the actual binding is set on the property not every time the value changes. Is there a way to register a callback for this or an event I need to listen to?

I have looked in MSDN on the classes DependencyProperty, DependencyObject, BindingOperations and DependencyPropertyDescriptor.

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

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

发布评论

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

评论(1

浅唱々樱花落 2024-12-02 08:59:09

我不认为有一个“官方方法”可以做到这一点,尽管几天前我遇到了同样的问题,并想出了一个相当愚蠢但至少有效的解决方法,

private bool isSet = false;

public static readonly DependencyProperty DummyProperty =
            DependencyProperty.Register("DummySource",
                                        typeof(DummyType),
                                        typeof(WhateverYouWant),
                                        new PropertyMetadata((s, a) =>
                                        {
                                          if (!isSet)
                                          {
                                            //Blah blah about what you wanna do

                                            isSet = true
                                          }
                                        }));

对我来说效果很好,也应该对你有用:)

I don't think there is an "official way" to do that, although I had the same problem some days ago and came up with a quite stupid but at least efficient workaround

private bool isSet = false;

public static readonly DependencyProperty DummyProperty =
            DependencyProperty.Register("DummySource",
                                        typeof(DummyType),
                                        typeof(WhateverYouWant),
                                        new PropertyMetadata((s, a) =>
                                        {
                                          if (!isSet)
                                          {
                                            //Blah blah about what you wanna do

                                            isSet = true
                                          }
                                        }));

Works fine for me, should do the trick for you as well :)

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