検索
ホームページバックエンド開発C++.NET の ObservableCollection に AddRange メソッドと INotifyCollectionChanging を追加する方法

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

.NET の

クラスは、項目が追加、削除、または再配置されたときに通知できるデータの動的なコレクションを提供します。ただし、ObservableCollection メソッドがないため、一度に複数のアイテムをバッチ追加できません。さらに、AddRange インターフェイスを使用して、コレクションにコミットされる前に変更を追跡し、変更をキャンセルする機会を提供できます。 INotifyCollectionChanging

ここでは、

ObservableCollection メソッドを実装し、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; }
}

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
このアプローチでは、変更が発生する前に追跡してキャンセルできると同時に、バッチ操作の利点も得られます。 コードは、より堅牢になり、

イベントの型安全性の問題を処理できるように改良されました。 また、カスタム イベントがトリガーされた後も基本クラスの INotifyCollectionChanging メソッドが呼び出されるようにするための OnCollectionChanging メソッドのオーバーライドも含まれており、これにより OnCollectionChanging の通常の機能が確保されます。 ObservableCollection

以上が.NET の ObservableCollection に AddRange メソッドと INotifyCollectionChanging を追加する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
C言語関数によって返される値の種類は何ですか?返品値を決定するものは何ですか?C言語関数によって返される値の種類は何ですか?返品値を決定するものは何ですか?Mar 03, 2025 pm 05:52 PM

この記事では、c関数のリターンタイプ、基本(int、float、charなど)、派生(配列、ポインター、構造体)、およびvoid型を含む詳細を示します。 コンパイラは、関数宣言とreturnステートメントを介して返品タイプを決定し、強制します

GULC:Cライブラリはゼロから構築されていますGULC:Cライブラリはゼロから構築されていますMar 03, 2025 pm 05:46 PM

GULCは、最小限のオーバーヘッド、積極的なインライン、およびコンパイラの最適化を優先する高性能Cライブラリです。 高周波取引や組み込みシステムなどのパフォーマンスクリティカルなアプリケーションに最適な設計では、シンプルさ、モジュールが強調されています

C言語関数の定義と呼び出しルールは何ですか、そしてC言語関数の定義と呼び出しルールは何ですか、そしてMar 03, 2025 pm 05:53 PM

この記事では、C関数宣言と定義、引数の合格(価値とポインターによる)、返品値、およびメモリリークやタイプの不一致などの一般的な落とし穴について説明します。 モジュール性とProviの宣言の重要性を強調しています

c言語関数形式文字ケース変換手順c言語関数形式文字ケース変換手順Mar 03, 2025 pm 05:53 PM

この記事では、文字列ケース変換のC関数について詳しく説明しています。 ctype.hのtoupper()とtolower()を使用し、文字列を介して繰り返し、ヌルターミネーターを処理することを説明しています。 ctype.hを忘れたり、文字列リテラルを変更するなどの一般的な落とし穴は

メモリに保存されているC言語関数の返品値はどこにありますか?メモリに保存されているC言語関数の返品値はどこにありますか?Mar 03, 2025 pm 05:51 PM

この記事では、C関数の戻り値ストレージを調べます。 通常、リターン値は通常、速度のためにレジスタに保存されます。値が大きいと、ポインターをメモリ(スタックまたはヒープ)に使用し、寿命に影響を与え、手動のメモリ管理が必要になります。直接acc

明確な使用法とフレーズ共有明確な使用法とフレーズ共有Mar 03, 2025 pm 05:51 PM

この記事では、形容詞の「個別」の多面的な使用法を分析し、その文法機能、一般的なフレーズ(例:「はっきりと異なる」とは異なる」、およびフォーマルと非公式の微妙なアプリケーションを調査します。

C標準テンプレートライブラリ(STL)はどのように機能しますか?C標準テンプレートライブラリ(STL)はどのように機能しますか?Mar 12, 2025 pm 04:50 PM

この記事では、C標準テンプレートライブラリ(STL)について説明し、そのコアコンポーネント(コンテナ、イテレーター、アルゴリズム、およびファンクター)に焦点を当てています。 これらが一般的なプログラミングを有効にし、コード効率を向上させ、読みやすさを改善する方法を詳述しています。

STL(ソート、検索、変換など)のアルゴリズムを効率的に使用するにはどうすればよいですか?STL(ソート、検索、変換など)のアルゴリズムを効率的に使用するにはどうすればよいですか?Mar 12, 2025 pm 04:52 PM

この記事では、cの効率的なSTLアルゴリズムの使用について詳しく説明しています。 データ構造の選択(ベクトル対リスト)、アルゴリズムの複雑さ分析(STD :: STD :: STD :: PARTIAL_SORTなど)、イテレーターの使用、および並列実行を強調しています。 のような一般的な落とし穴

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

SecLists

SecLists

SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境