INotifyPropertyChanged causes cross-thread error

后端 未结 4 1788
走了就别回头了
走了就别回头了 2020-12-16 06:21

Here is my scenarion:

I have a GridControl bound to a BindingList. At first what I was doing was creating a worker thread and access the BindingList directly, but th

4条回答
  •  眼角桃花
    2020-12-16 06:35

    I subclassed BindingList so I could check for a required Invoke. This way my business objects do not have a reference to the UI.

    public class InvokingBindingList : BindingList
    {
      public InvokingBindingList(IList list, Control control = null) : base(list)
      {
        this.Control = control;
      }
    
      public InvokingBindingList(Control control = null)
      {
        this.Control = control;
      }
    
      public Control Control { get; set; }
    
      protected override void OnListChanged(ListChangedEventArgs e)
      {
        if (Control?.InvokeRequired == true)
          Control.Invoke(new Action(() => base.OnListChanged(e)));
        else
          base.OnListChanged(e);
      }
    }
    

提交回复
热议问题