|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace System.Collections.ObjectModel
{
/// <summary>
/// Read-only wrapper around an ObservableCollection.
/// </summary>
[Serializable]
[DebuggerTypeProxy(typeof(CollectionDebugView<>))]
[DebuggerDisplay("Count = {Count}")]
[TypeForwardedFrom("WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
public class ReadOnlyObservableCollection<T> : ReadOnlyCollection<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
/// <summary>
/// Initializes a new instance of ReadOnlyObservableCollection that
/// wraps the given ObservableCollection.
/// </summary>
public ReadOnlyObservableCollection(ObservableCollection<T> list) : base(list)
{
((INotifyCollectionChanged)Items).CollectionChanged += new NotifyCollectionChangedEventHandler(HandleCollectionChanged);
((INotifyPropertyChanged)Items).PropertyChanged += new PropertyChangedEventHandler(HandlePropertyChanged);
}
/// <summary>Gets an empty <see cref="ReadOnlyObservableCollection{T}"/>.</summary>
/// <value>An empty <see cref="ReadOnlyObservableCollection{T}"/>.</value>
/// <remarks>The returned instance is immutable and will always be empty.</remarks>
public static new ReadOnlyObservableCollection<T> Empty { get; } = new ReadOnlyObservableCollection<T>(new ObservableCollection<T>());
/// <summary>
/// CollectionChanged event (per <see cref="INotifyCollectionChanged" />).
/// </summary>
event NotifyCollectionChangedEventHandler? INotifyCollectionChanged.CollectionChanged
{
add => CollectionChanged += value;
remove => CollectionChanged -= value;
}
/// <summary>
/// Occurs when the collection changes, either by adding or removing an item.
/// </summary>
/// <remarks>
/// see <seealso cref="INotifyCollectionChanged"/>
/// </remarks>
[field: NonSerialized]
protected virtual event NotifyCollectionChangedEventHandler? CollectionChanged;
/// <summary>
/// raise CollectionChanged event to any listeners
/// </summary>
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
{
CollectionChanged?.Invoke(this, args);
}
/// <summary>
/// PropertyChanged event (per <see cref="INotifyPropertyChanged" />).
/// </summary>
event PropertyChangedEventHandler? INotifyPropertyChanged.PropertyChanged
{
add => PropertyChanged += value;
remove => PropertyChanged -= value;
}
/// <summary>
/// Occurs when a property changes.
/// </summary>
/// <remarks>
/// see <seealso cref="INotifyPropertyChanged"/>
/// </remarks>
[field: NonSerialized]
protected virtual event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// raise PropertyChanged event to any listeners
/// </summary>
protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
{
PropertyChanged?.Invoke(this, args);
}
private void HandleCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
OnCollectionChanged(e);
}
private void HandlePropertyChanged(object? sender, PropertyChangedEventArgs e)
{
OnPropertyChanged(e);
}
}
}
|