gpt4 book ai didi

c# - 我可以以某种方式暂时禁用 WPF 数据绑定(bind)更改吗?

转载 作者:可可西里 更新时间:2023-11-01 03:06:03 24 4
gpt4 key购买 nike

我有一个使用 MVVM 数据绑定(bind)的 WPF 应用程序。我正在将项目添加到 ObservableCollection<...>确实有很多。

现在我想知道每次我向集合中添加一个时,它是否会立即触发事件并造成不必要的开销?如果是这样,我能否以某种方式暂时禁用事件通知并在我的代码末尾手动触发一次,这样如果我添加 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) { }
}
}

我现在收到这个错误:

Additional information: Range actions are not supported.

错误就在这里:

OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collection.ToList()));

最佳答案

ObservableCollection 的这个扩展很容易解决这个问题。

它公开了一个公共(public)的 SupressNotification 属性,以允许用户控制何时抑制 CollectionChanged 通知。

它不提供范围插入/删除,但如果 CollectionChanged 通知被抑制,在大多数情况下对集合进行范围操作的需要会减少。

此实现将所有被抑制的通知替换为重置通知。这在逻辑上是合理的。当用户抑制通知,进行批量更改然后重新启用它时,应该适当发送一个重新发送通知。

public class ObservableCollectionEx<T> : ObservableCollection<T>
{
private bool _notificationSupressed = false;
private bool _supressNotification = false;
public bool SupressNotification
{
get
{
return _supressNotification;
}
set
{
_supressNotification = value;
if (_supressNotification == false && _notificationSupressed)
{
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
_notificationSupressed = false;
}
}
}

protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (SupressNotification)
{
_notificationSupressed = true;
return;
}
base.OnCollectionChanged(e);
}
}

关于c# - 我可以以某种方式暂时禁用 WPF 数据绑定(bind)更改吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10572522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com