private void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
// Remove handler for oldValue.CollectionChanged (if present)
INotifyCollectionChanged oldValueINotifyCollectionChanged = oldValue as INotifyCollectionChanged;
if(null != oldValueINotifyCollectionChanged && null != _collectionChangedWeakEventListener)
{
_collectionChangedWeakEventListener.Detach();
_collectionChangedWeakEventListener = null;
}
// Add handler for newValue.CollectionChanged (if possible)
INotifyCollectionChanged newValueINotifyCollectionChanged = newValue as INotifyCollectionChanged;
if(null != newValueINotifyCollectionChanged)
{
_collectionChangedWeakEventListener = new WeakEventListener<AutoCompleteBox, object, NotifyCollectionChangedEventArgs>(this) { OnEventAction = (instance, source, eventArgs) => instance.ItemsSourceCollectionChanged(eventArgs), OnDetachAction = weakEventListener => newValueINotifyCollectionChanged.CollectionChanged -= weakEventListener.OnEvent };
newValueINotifyCollectionChanged.CollectionChanged += _collectionChangedWeakEventListener.OnEvent;
}
// Store a local cached copy of the data
_items = newValue == null ? null : new List<object>(newValue.Cast<object>().ToList());
// Clear and set the view on the selection adapter
ClearView();
// ReSharper disable once PossibleUnintendedReferenceComparison
if(SelectionAdapter != null && SelectionAdapter.ItemsSource != _view)
{
SelectionAdapter.ItemsSource = _view;
}
if(IsDropDownOpen)
{
RefreshView();
}
}