views:

30

answers:

1

Hi, I need to create a dependency property on a custom control. The problem is that the propertyChangedCallback does not get called when the collection changes. How should I properly handle this scenario?

I am afraid of memory leaks caused by simply event hooking to the CollectionChanged event during the initial property change (when the observable collection is assigned to the dependency property).

In other words, I need to create another property like ItemsControl.ItemsSource.

I've looked to the source of ItemsControl.ItemsSource using Reflector. Internally it uses WeakCollectionChangedListener which is internal class. I probably could copy its source and make use of it but i believe there must be another (better) solution.

A: 

There is a good in-depth post on Delay's Blog on implementing the WeakEvent pattern in Silverlight for this exact scenario (changing a collection source which implements INotifyCollectionChanged).

He provides code for a WeakEventListener implementation as well.

Dan Auclair