WPF: what's the most efficient/fast way of adding items to a ListView?

前端 未结 1 636
花落未央
花落未央 2020-12-13 07:44

I need to display lots of rows in a grid added at a pretty high frequency (up to 10 rows per second in some cases) I chose ListView because I assume is the fastest grid cont

1条回答
  •  一生所求
    2020-12-13 07:57

    Instead of using ObservableCollection I will suggest BindingList class, you can do something like this..

    BindingList list = new BindingList();
    
    list.AllowEdit = true;
    list.AllowNew = true;
    list.AllowRemove = true;
    
    // set the list as items source
    itemCollection.ItemsSource = list;
    
    // add many items...
    
    // disable UI updation
    list.RaiseListChangedEvents = false;
    
    for each(string s in MyCollection){
       list.Add(s);
    }
    
    // after all.. update the UI with following
    list.RaiseListChangedEvents = true;
    list.ResetBindings(); // this forces update of entire list
    

    You can enable/disable updating even in batches, instead of adding everything at one shot, BindingList has been functioning better then ObservableCollection in all my UI, I wonder why everywhere people talk more about ObservableCollection when BindingList really superseds ObservableCollection.

    0 讨论(0)
提交回复
热议问题