Observer pattern in the WPF
The ObservableCollection
can be considered as a data structure, which leverages the observer pattern to provide notifications when items get added or removed, or when the whole list is refreshed:
class ObservableDataSource { public ObservableCollection<string> data { get; set; } public ObservableDataSource() { data = new ObservableCollection<string>(); data.CollectionChanged += OnCollectionChanged; } void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) { Console.WriteLine("The Data got changed ->" + args.NewItems[0]); } public void TearDown() { data.CollectionChanged -= OnCollectionChanged; } }
The following code snippet creates an instance of an Observableconnection
based...