开发者

How can I know that the binding of a dependency property has changed?

开发者 https://www.devze.com 2023-03-22 07:21 出处:网络
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:

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 notif开发者_Python百科ied 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.


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消