Take the 2-minute tour ×

I was looking in the framework for an implementation of WeakEventManager that listens for changes to DependencyProperties. I'm a bit confused by the fact that the only weak property change event listener I find, the PropertyChangedEventManager, is designed to be used on types that implement INotifyPropertyChanged.

Does this mean that if you listen to a DependencyProperty for changes

    .FromProperty(target, target.OwnerType)
    .AddValueChanged(component, handler)

that I don't have to worry about leaking instances who are kept alive by event registration?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

DependencyPropertyDescriptor leaks big time, I had lot of issues because of it. Unless you explicitly call RemoveValueChanged all your components will be rooted. Internally it maintains a HashTable of EventHandler. Here is what it does:

if (this.valueChangedHandlers == null)
  this.valueChangedHandlers = new Hashtable();
EventHandler eventHandler = (EventHandler) this.valueChangedHandlers[component];
this.valueChangedHandlers[component] = (object) Delegate.Combine((Delegate) eventHandler, (Delegate) handler);

Since property descriptors are cached, all your components will be rooted.

share|improve this answer
Bugger all. Got a better solution? –  Will Jul 21 '11 at 18:33
A siemple and manual solution is to explicitly call RemoveValueChanged when stop listening changes. –  David Oliván Ubieto Oct 28 at 10:53

Your Answer


