मेरे पास एक 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, कोई जानकारी नहीं है। इसे अंतर्निहित बनाना बहुत अच्छा होगा, लेकिन ... हो सकता है, एरिक लिपर्ट कभी-कभी कहता है: क्योंकि इसे लागू नहीं किया गया ... – Tigran
मैं उन्हें काम करने में असमर्थ हूं, मुझे मिलता है: 'अतिरिक्त जानकारी : कन्स्ट्रक्टर केवल 'रीसेट' कार्रवाई का समर्थन करता है।'जब कोड' ऑनकॉलेक्शन चेंज किया जाता है (नया अधिसूचना कोलेक्शन चेंजएडएन्टएआरजीएस (नोटिफ़ाई कोलेक्शन चेंजएड एक्शन। जोड़ें); '। – Tower