diff --git a/MultiSelectTreeView/Controls/MultiSelectTreeViewItem.cs b/MultiSelectTreeView/Controls/MultiSelectTreeViewItem.cs
index 80aab5e..d68a101 100644
--- a/MultiSelectTreeView/Controls/MultiSelectTreeViewItem.cs
+++ b/MultiSelectTreeView/Controls/MultiSelectTreeViewItem.cs
@@ -1,4 +1,6 @@
-using System.Collections.Generic;
+using System.Collections;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Windows.Automation.Peers;
@@ -663,10 +665,25 @@ protected override void OnMouseDown(MouseButtonEventArgs e)
}
}
- protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
- {
+ ///
+ /// Create a direct subscription to the collection instead of relying on the internal mechanisms of .
+ /// This avoids the flood of events on tree population created by WPF.
+ ///
+ ///
+ ///
+ protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
+ {
+ if (newValue is INotifyCollectionChanged notifyCollection)
+ {
+ notifyCollection.CollectionChanged += (a, e) => OwnItemsChanged(e);
+ //notifyCollection.CollectionChanged += (a, e) => base.OnItemsChanged(e);
+ }
+ base.OnItemsSourceChanged(oldValue, newValue);
+ }
+
+ private void OwnItemsChanged(NotifyCollectionChangedEventArgs e)
+ {
MultiSelectTreeView parentTV;
-
switch (e.Action)
{
case NotifyCollectionChangedAction.Remove:
@@ -696,7 +713,7 @@ protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
parentTV = ParentTreeView;
if (parentTV == null)
parentTV = lastParentTreeView;
- if (parentTV != null)
+ if (parentTV != null && parentTV.SelectedItems.Count > 0)
{
var selection = new object[parentTV.SelectedItems.Count];
parentTV.SelectedItems.CopyTo(selection, 0);
@@ -713,8 +730,6 @@ protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
}
break;
}
-
- base.OnItemsChanged(e);
}
#endregion Protected methods