MVVM 데이터 바인딩을 사용하는 WPF 응용 프로그램이 있습니다. ObservableCollection<...>
에 항목을 추가하고 있으며 그 중 상당수는 참으로 많습니다.WPF 데이터 바인딩 변경을 일시적으로 사용하지 않도록 설정할 수 있습니까?
이제 컬렉션에 하나를 추가 할 때마다 즉시 이벤트가 실행되고 불필요한 오버 헤드가 발생하는지 궁금합니다. 그렇다면 어떻게 든 일시적으로 이벤트 알림을 사용 중지하고 코드 끝에 한 번 수동으로 실행하면 10k 항목을 추가하면 10k 회가 아닌 한 번만 발동됩니다.
업데이트 :
using System;
using System.Linq;
using System.Collections.Specialized;
using System.Collections.Generic;
namespace MyProject
{
/// <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 ObservableCollection<T> : System.Collections.ObjectModel.ObservableCollection<T>
{
/// <summary>
/// Adds the elements of the specified collection to the end of the ObservableCollection(Of T).
/// </summary>
public void AddRange(IEnumerable<T> collection)
{
foreach (var i in collection) Items.Add(i);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collection.ToList()));
}
/// <summary>
/// Removes the first occurence of each item in the specified collection from ObservableCollection(Of T).
/// </summary>
public void RemoveRange(IEnumerable<T> collection)
{
foreach (var i in collection) Items.Remove(i);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, collection.ToList()));
}
/// <summary>
/// Clears the current collection and replaces it with the specified item.
/// </summary>
public void Replace(T item)
{
ReplaceRange(new T[] { item });
}
/// <summary>
/// Clears the current collection and replaces it with the specified collection.
/// </summary>
public void ReplaceRange(IEnumerable<T> collection)
{
List<T> old = new List<T>(Items);
Items.Clear();
foreach (var i in collection) Items.Add(i);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, collection.ToList()));
}
/// <summary>
/// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class.
/// </summary>
public ObservableCollection() : base() { }
/// <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 ObservableCollection(IEnumerable<T> collection) : base(collection) { }
}
}
지금이 오류를 얻을 :
추가 정보 : 범위 작업은 지원되지 않습니다 나는이 클래스를 가진 시도했다.
오류
여기에 온다 :OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collection.ToList()));
흥미 롭습니다. 이것이 왜 ObservableCollection의 일부가 아닌지 궁금합니다. – Tower
@rFactor : honeslty, 전혀 모르겠습니다. 내장되어있는 것처럼 좋으면 좋겠지 만 ... lke일지도 모릅니다. 에릭 리 퍼트 (Eric Lippert)는 때때로 다음과 같이 말합니다 : 구현하지 않았기 때문에 ... – Tigran
나는 그 (것)들을 얻을 수 없습니다, 나는 얻는다 :'추가 정보 : 생성자는 '재설정'작업 만 지원합니다.'코드가'OnCollectionChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add));)를 호출 할 때. – Tower