BindingList<> ListChanged event

前端 未结 1 1559
春和景丽
春和景丽 2020-12-23 06:15

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. It is m

相关标签:
1条回答
  • 2020-12-23 06:40

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题