Skip to content

Instantly share code, notes, and snippets.

@LoriBru
Created January 24, 2019 13:52
Show Gist options
  • Select an option

  • Save LoriBru/2027393fef9c649054f305d44c7bdddd to your computer and use it in GitHub Desktop.

Select an option

Save LoriBru/2027393fef9c649054f305d44c7bdddd to your computer and use it in GitHub Desktop.
// <summary>
/// This class adds the ability to refresh the list when any property of
/// the objects changes in the list which implements the INotifyPropertyChanged.
/// </summary>
/// <typeparam name="T">
public class ObservableCollectionEx<T> : ObservableCollection<T>
{
// Override the vent so this class can access it
public override event NotifyCollectionChangedEventHandler CollectionChanged;
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
using (BlockReentrancy())
{
var eventHanlder = CollectionChanged;
if (eventHanlder == null)
return;
var delegates = eventHanlder.GetInvocationList();
// Go through the invocation list
foreach (NotifyCollectionChangedEventHandler handler in delegates)
{
var dispatcherObject = handler.Target as DispatcherObject;
// If the subscriber is a DispatcherObject and different thread do this:
if (dispatcherObject != null && !dispatcherObject.CheckAccess())
{
// Invoke handler in the target dispatcher's thread
dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, e);
}
// Else, execute handler as is
else
{
handler(this, e);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment