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 ObservableDataSource ...

Get .NET Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.