Home Backend Development C++ How to Add an AddRange Method and INotifyCollectionChanging to an ObservableCollection in .NET?

How to Add an AddRange Method and INotifyCollectionChanging to an ObservableCollection in .NET?

Jan 20, 2025 am 07:22 AM

How to Add an AddRange Method and INotifyCollectionChanging to an ObservableCollection in .NET?

The

class in .NET provides a dynamic collection of data that can be notified when items are added, removed, or rearranged. However, it lacks the ObservableCollection method, which cannot batch add multiple items at once. Additionally, the AddRange interface can be used to track changes before they are committed to the collection, providing the opportunity to cancel the changes. INotifyCollectionChanging

Here's how to implement the

method in ObservableCollection and integrate AddRange. INotifyCollectionChanging

C#:

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.Specialized;

public class ObservableRangeCollection<T> : ObservableCollection<T>, INotifyCollectionChanging<T>
{
    public event NotifyCollectionChangingEventHandler<T> CollectionChanging;

    protected override void OnCollectionChanging(NotifyCollectionChangingEventArgs e)
    {
        // 创建一个包含更改信息的事件参数
        var typedEventArgs = new NotifyCollectionChangingEventArgs<T>(e.Action, e.NewItems != null ? (IEnumerable<T>)e.NewItems : null, e.OldItems != null ? (IEnumerable<T>)e.OldItems : null, e.Index);

        // 触发自定义事件,允许取消操作
        CollectionChanging?.Invoke(this, typedEventArgs);

        // 检查是否取消
        if (typedEventArgs.Cancel)
        {
            return;
        }

        // 调用基类的OnCollectionChanging方法
        base.OnCollectionChanging(e);
    }

    public void AddRange(IEnumerable<T> collection)
    {
        if (collection == null)
        {
            throw new ArgumentNullException(nameof(collection));
        }

        // 触发CollectionChanging事件
        OnCollectionChanging(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collection));

        // 添加项目
        foreach (T item in collection)
        {
            Add(item);
        }
    }
}

//自定义INotifyCollectionChanging接口,用于类型安全
public interface INotifyCollectionChanging<T> : INotifyCollectionChanged
{
    event NotifyCollectionChangingEventHandler<T> CollectionChanging;
}

//自定义事件处理程序,用于类型安全
public delegate void NotifyCollectionChangingEventHandler<T>(object sender, NotifyCollectionChangingEventArgs<T> e);

//自定义事件参数,用于类型安全
public class NotifyCollectionChangingEventArgs<T> : NotifyCollectionChangedEventArgs
{
    public NotifyCollectionChangingEventArgs(NotifyCollectionChangedAction action, IEnumerable<T> newItems) : base(action, newItems) { }
    public NotifyCollectionChangingEventArgs(NotifyCollectionChangedAction action, IEnumerable<T> newItems, IEnumerable<T> oldItems) : base(action, newItems, oldItems) { }
    public NotifyCollectionChangingEventArgs(NotifyCollectionChangedAction action, IEnumerable<T> newItems, int index) : base(action, newItems, index) { }
    public NotifyCollectionChangingEventArgs(NotifyCollectionChangedAction action, T newItem, int index) : base(action, newItem, index) { }
    public NotifyCollectionChangingEventArgs(NotifyCollectionChangedAction action, T oldItem, int index) : base(action, null, oldItem, index) { }
    public bool Cancel { get; set; }
}
Copy after login

VB.NET:

Imports System
Imports System.Collections.ObjectModel
Imports System.Collections.Generic
Imports System.Collections.Specialized
Imports System.ComponentModel

Public Class ObservableRangeCollection(Of T)
    Inherits ObservableCollection(Of T)
    Implements INotifyCollectionChanging(Of T)

    Public Event CollectionChanging As NotifyCollectionChangingEventHandler(Of T) Implements INotifyCollectionChanging(Of T).CollectionChanging

    Protected Overrides Sub OnCollectionChanging(e As NotifyCollectionChangedEventArgs)
        Dim typedEventArgs As New NotifyCollectionChangingEventArgs(Of T)(e.Action, If(e.NewItems IsNot Nothing, DirectCast(e.NewItems, IEnumerable(Of T)), Nothing), If(e.OldItems IsNot Nothing, DirectCast(e.OldItems, IEnumerable(Of T)), Nothing), e.Index)
        RaiseEvent CollectionChanging(Me, typedEventArgs)
        If typedEventArgs.Cancel Then
            Return
        End If
        MyBase.OnCollectionChanging(e)
    End Sub

    Public Sub AddRange(collection As IEnumerable(Of T))
        If collection Is Nothing Then
            Throw New ArgumentNullException("collection")
        End If

        OnCollectionChanging(New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collection))

        For Each item As T In collection
            Add(item)
        Next
    End Sub
End Class

'自定义INotifyCollectionChanging接口,用于类型安全
Public Interface INotifyCollectionChanging(Of T)
    Inherits INotifyCollectionChanged
    Event CollectionChanging As NotifyCollectionChangingEventHandler(Of T)
End Interface

'自定义事件处理程序,用于类型安全
Public Delegate Sub NotifyCollectionChangingEventHandler(Of T)(sender As Object, e As NotifyCollectionChangingEventArgs(Of T))

'自定义事件参数,用于类型安全
Public Class NotifyCollectionChangingEventArgs(Of T)
    Inherits NotifyCollectionChangedEventArgs
    Public Sub New(action As NotifyCollectionChangedAction, newItems As IEnumerable(Of T))
        MyBase.New(action, newItems)
    End Sub
    Public Sub New(action As NotifyCollectionChangedAction, newItems As IEnumerable(Of T), oldItems As IEnumerable(Of T))
        MyBase.New(action, newItems, oldItems)
    End Sub
    Public Sub New(action As NotifyCollectionChangedAction, newItems As IEnumerable(Of T), index As Integer)
        MyBase.New(action, newItems, index)
    End Sub
    Public Sub New(action As NotifyCollectionChangedAction, newItem As T, index As Integer)
        MyBase.New(action, newItem, index)
    End Sub
    Public Sub New(action As NotifyCollectionChangedAction, oldItem As T, index As Integer)
        MyBase.New(action, Nothing, oldItem, index)
    End Sub
    Public Property Cancel As Boolean
End Class
Copy after login
With this approach, you benefit from batch operations while being able to track and cancel changes before they occur. The code has been improved to make it more robust and handle type safety issues for

events. It also includes an override of the INotifyCollectionChanging method to ensure that after the custom event is triggered, the base class's OnCollectionChanging method is still called, thus ensuring the normal functionality of OnCollectionChanging. ObservableCollection

The above is the detailed content of How to Add an AddRange Method and INotifyCollectionChanging to an ObservableCollection in .NET?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the types of values ​​returned by c language functions? What determines the return value? What are the types of values ​​returned by c language functions? What determines the return value? Mar 03, 2025 pm 05:52 PM

What are the types of values ​​returned by c language functions? What determines the return value?

Gulc: C library built from scratch Gulc: C library built from scratch Mar 03, 2025 pm 05:46 PM

Gulc: C library built from scratch

C language function format letter case conversion steps C language function format letter case conversion steps Mar 03, 2025 pm 05:53 PM

C language function format letter case conversion steps

What are the definitions and calling rules of c language functions and what are the What are the definitions and calling rules of c language functions and what are the Mar 03, 2025 pm 05:53 PM

What are the definitions and calling rules of c language functions and what are the

Where is the return value of the c language function stored in memory? Where is the return value of the c language function stored in memory? Mar 03, 2025 pm 05:51 PM

Where is the return value of the c language function stored in memory?

distinct usage and phrase sharing distinct usage and phrase sharing Mar 03, 2025 pm 05:51 PM

distinct usage and phrase sharing

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? How do I use algorithms from the STL (sort, find, transform, etc.) efficiently? Mar 12, 2025 pm 04:52 PM

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?

How does the C   Standard Template Library (STL) work? How does the C Standard Template Library (STL) work? Mar 12, 2025 pm 04:50 PM

How does the C Standard Template Library (STL) work?

See all articles