Created
January 24, 2019 13:52
-
-
Save LoriBru/2027393fef9c649054f305d44c7bdddd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // <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