2023-07-03 01:51:09 +08:00
using System.Collections.ObjectModel ;
2022-05-23 00:58:58 +08:00
using System.Collections.Specialized ;
2023-07-03 01:51:09 +08:00
namespace CMM.Library.Base ;
/// <summary>
/// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
/// </summary>
/// <typeparam name="T"></typeparam>
public class ObservableRangeCollection < T > : ObservableCollection < T >
2022-05-23 00:58:58 +08:00
{
/// <summary>
2023-07-03 01:51:09 +08:00
/// Adds the elements of the specified collection to the end of the ObservableCollection(Of T).
2022-05-23 00:58:58 +08:00
/// </summary>
2023-07-03 01:51:09 +08:00
public void AddRange ( IEnumerable < T > collection )
2022-05-23 00:58:58 +08:00
{
2023-07-03 01:51:09 +08:00
if ( collection = = null ) throw new ArgumentNullException ( "collection" ) ;
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
foreach ( var i in collection ) Items . Add ( i ) ;
OnCollectionChanged ( new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Reset ) ) ;
}
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
/// <summary>
/// Removes the first occurence of each item in the specified collection from ObservableCollection(Of T).
/// </summary>
public void RemoveRange ( IEnumerable < T > collection )
{
if ( collection = = null ) throw new ArgumentNullException ( "collection" ) ;
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
foreach ( var i in collection ) Items . Remove ( i ) ;
OnCollectionChanged ( new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Reset ) ) ;
}
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
/// <summary>
/// Clears the current collection and replaces it with the specified item.
/// </summary>
public void Replace ( T item )
{
ReplaceRange ( new T [ ] { item } ) ;
}
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
/// <summary>
/// Clears the current collection and replaces it with the specified collection.
/// </summary>
public void ReplaceRange ( IEnumerable < T > collection )
{
if ( collection = = null ) throw new ArgumentNullException ( "collection" ) ;
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
Items . Clear ( ) ;
foreach ( var i in collection ) Items . Add ( i ) ;
OnCollectionChanged ( new NotifyCollectionChangedEventArgs ( NotifyCollectionChangedAction . Reset ) ) ;
}
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
/// <summary>
/// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class.
/// </summary>
public ObservableRangeCollection ( )
: base ( ) { }
2022-05-23 00:58:58 +08:00
2023-07-03 01:51:09 +08:00
/// <summary>
/// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class that contains elements copied from the specified collection.
/// </summary>
/// <param name="collection">collection: The collection from which the elements are copied.</param>
/// <exception cref="System.ArgumentNullException">The collection parameter cannot be null.</exception>
public ObservableRangeCollection ( IEnumerable < T > collection )
: base ( collection ) { }
2022-05-23 00:58:58 +08:00
}