暂停对绑定 ObservableCollection的 DataGrid 的更新
有没有办法暂停notifyCollectionChanged
observableCollection
的事件?我认为以下内容:
public class PausibleObservableCollection<Message> : ObservableCollection<Message>
{
public bool IsBindingPaused { get; set; }
protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (!IsBindingPaused)
base.OnCollectionChanged(e);
}
}
这确实暂停了通知,但是显然,当时被排除的(但仍添加)的项目在notifyCollectionChangeDeventargs
中,因此当我启用通知时未传递到bond datagrid再次。
为了控制这一方面,我是否必须提出定制的集合实现?
Is there a way to pause the NotifyCollectionChanged
event of an ObservableCollection
? I thought something like the following:
public class PausibleObservableCollection<Message> : ObservableCollection<Message>
{
public bool IsBindingPaused { get; set; }
protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (!IsBindingPaused)
base.OnCollectionChanged(e);
}
}
This pauses the notification indeed, but obviously the then left out (but still added) items are within the NotifyCollectionChangedEventArgs
and are therefore not passed to the bound DataGrid when I enable the notification again.
Will I have to come up with a custom implementation of a collection in order to control this aspect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不想丢失临时存储可能起作用的任何通知,则以下方法可能有效,但未经测试:
这应该将集合暂停时发生的每个更改推送到队列中,一旦集合设置为恢复,队列就会被清空。
If you do not want to loose any notifications a temporary storage might work, the following might work but is untested:
This should push every change that happens while the collection is paused into a queue, which then is emptied once the collection is set to resume.
为了配合@HB的答案(我在他/她发布时进行测试) - 您可以通过 NotifyCollectionChangedAction.Reset 将操作更改为 CollectionChanged 事件的事件参数。请注意,这对于大型集合来说效率不高。
To go along with @H.B's answer (I was testing while he/she posted) - you can pass the NotifyCollectionChangedAction.Reset Change Action as an event argument to the CollectionChanged event. Note that this will not be efficient on large collections.