的BindingList<> ListChanged事件 [英] BindingList<> ListChanged event

查看:595
本文介绍了的BindingList<> ListChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的BindingList<一类设置为BindingSource的,而这又是设置一个DataGridView的DataSource属性的DataSource属性>

I have a BindingList<> of a class set to the DataSource property of a BindingSource, which is in turn set to the DataSource property of a DataGridView.

1。 这是我的理解是,任何增加的名单将触发一个ListChanged事件将传播通过BindingSource的,然后到DataGridView中,这将自动更新,显示更改。这不会发生,因为该事件已经自动挂接。 (什么事?)

1. It is my understanding that any additions to the list will fire a ListChanged event which will propagate through the BindingSource and then onto the DataGridView, which will update itself to display the change. This will happen because the events have been automatically hooked up. (Yes?)

这是一切优秀和良好的,当所有的工作都是在UI线程上完成的,而是创建并从非UI线程改变,列表时当电网更新最终发生跨线程异常。我可以理解为什么会这样,但如何解决它?

This is all fine and good when all the work is done on the UI thread, but when the list is created and changed from a non-UI thread, ultimately a cross-thread exception occurs when the grid is updated. I can understand why this happens, but no how to fix it...

2。 我有多么艰难的时间理解,就是要我最好的拦截ListChanged事件,试图元帅的东西到UI线程?我猜测,我需要一个引用到UI线程以某种方式来帮助做到这一点?

2. What I am having a tough time understanding, is where should I best intercept the ListChanged event to try and marshal things onto the UI thread? I am guessing that I need a reference to the UI thread somehow to help do this?

我看过这么多的帖子/文章,但我挣扎,因为我不完全理解在这里发挥的机制。

I have read many posts/articles on this, but I'm struggling because I don't fully understand the mechanisms at work here.

我将永远不会被改变的任何项目,一旦他们在列表中,仅将其添加,初步清除列表。

I will never be changing any items once they are in the list, only adding them, and initially clearing the list.

(我使用.NET 2.0)

(I am using .NET 2.0)

推荐答案

您可以扩展的BindingList使用ISynchronizeInvoke(由System.Windows.Forms.Control的实现)当元帅的事件invokations到UI线程。

You can extend BindingList to use an ISynchronizeInvoke (implemented by System.Windows.Forms.Control) to marshal the event invokations onto the UI thread.

然后,所有你需要做的就是使用新的列表类型,并全部进行排序。

Then all you need to do is use the new list type and all is sorted.

public partial class Form1 : System.Windows.Forms.Form {

    SyncList<object> _List; 
    public Form1() {
        InitializeComponent();
        _List = new SyncList<object>(this);
    }
}

public class SyncList<T> : System.ComponentModel.BindingList<T> {

    private System.ComponentModel.ISynchronizeInvoke _SyncObject;
    private System.Action<System.ComponentModel.ListChangedEventArgs> _FireEventAction;

    public SyncList() : this(null) {
    }

    public SyncList(System.ComponentModel.ISynchronizeInvoke syncObject) {

        _SyncObject = syncObject;
        _FireEventAction = FireEvent;
    }

    protected override void OnListChanged(System.ComponentModel.ListChangedEventArgs args) {
        if(_SyncObject == null) {
            FireEvent(args);
        }
        else {
            _SyncObject.Invoke(_FireEventAction, new object[] {args});
        }
    }

    private void FireEvent(System.ComponentModel.ListChangedEventArgs args) {
        base.OnListChanged(args);
    }
}

这篇关于的BindingList&LT;&GT; ListChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆