//--------------------------------------------------------------------------- // // // Copyright (C) 2003 by Microsoft Corporation. All rights reserved. // // // // Description: Read-only wrapper around an ObservableCollection. // // See spec at http://avalon/connecteddata/Specs/Collection%20Interfaces.mht // //--------------------------------------------------------------------------- using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; using System.Runtime.CompilerServices; namespace System.Collections.ObjectModel { /// /// Read-only wrapper around an ObservableCollection. /// #if !FEATURE_NETCORE [Serializable()] #if !MOBILE [TypeForwardedFrom("WindowsBase, Version=3.0.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")] #endif #endif public class ReadOnlyObservableCollection : ReadOnlyCollection, INotifyCollectionChanged, INotifyPropertyChanged { #region Constructors //------------------------------------------------------ // // Constructors // //------------------------------------------------------ /// /// Initializes a new instance of ReadOnlyObservableCollection that /// wraps the given ObservableCollection. /// public ReadOnlyObservableCollection(ObservableCollection list) : base(list) { ((INotifyCollectionChanged)Items).CollectionChanged += new NotifyCollectionChangedEventHandler(HandleCollectionChanged); ((INotifyPropertyChanged)Items).PropertyChanged += new PropertyChangedEventHandler(HandlePropertyChanged); } #endregion Constructors #region Interfaces //------------------------------------------------------ // // Interfaces // //------------------------------------------------------ #region INotifyCollectionChanged /// /// CollectionChanged event (per ). /// event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged { add { CollectionChanged += value; } remove { CollectionChanged -= value; } } /// /// Occurs when the collection changes, either by adding or removing an item. /// /// /// see /// #if !FEATURE_NETCORE [field:NonSerializedAttribute()] #endif protected virtual event NotifyCollectionChangedEventHandler CollectionChanged; /// /// raise CollectionChanged event to any listeners /// protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args) { if (CollectionChanged != null) { CollectionChanged(this, args); } } #endregion INotifyCollectionChanged #region INotifyPropertyChanged /// /// PropertyChanged event (per ). /// event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged { add { PropertyChanged += value; } remove { PropertyChanged -= value; } } /// /// Occurs when a property changes. /// /// /// see /// #if !FEATURE_NETCORE [field:NonSerializedAttribute()] #endif protected virtual event PropertyChangedEventHandler PropertyChanged; /// /// raise PropertyChanged event to any listeners /// protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) { if (PropertyChanged != null) { PropertyChanged(this, args); } } #endregion INotifyPropertyChanged #endregion Interfaces #region Private Methods //------------------------------------------------------ // // Private Methods // //------------------------------------------------------ // forward CollectionChanged events from the base list to our listeners void HandleCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { OnCollectionChanged(e); } // forward PropertyChanged events from the base list to our listeners void HandlePropertyChanged(object sender, PropertyChangedEventArgs e) { OnPropertyChanged(e); } #endregion Private Methods #region Private Fields //------------------------------------------------------ // // Private Fields // //------------------------------------------------------ #endregion Private Fields } }