Insert mutiple items in ObservableCollection<t></t>
-
Is it possible to insert multiple items in
ObservableCollection
? I have about 5000 items and inserting it one by one causing UI to go very slow. Thanks.How are they stored at the moment? in a List? You can convert a List to an ObservableCollection thru its constructor: ObservableCollection<object> Col = new ObservableCollection<object>(originalList); You can concatenate one sequence to another with: Collection1 = Collection1.Concat(Collection2); Hope this helps.
Dawn is nature's way of telling you to go to bed.
-
How are they stored at the moment? in a List? You can convert a List to an ObservableCollection thru its constructor: ObservableCollection<object> Col = new ObservableCollection<object>(originalList); You can concatenate one sequence to another with: Collection1 = Collection1.Concat(Collection2); Hope this helps.
Dawn is nature's way of telling you to go to bed.
Thanks a lot. First one is good enough at system startup when a lot of items received but unusable during runtime. Second soluction return IEnumerable. So i need to use ctor after that. This not i'm actually want. If system running and i'll recreate object collection i'm also need to recreate all bindings to it. It's seems imposssible :-)
-
Is it possible to insert multiple items in
ObservableCollection
? I have about 5000 items and inserting it one by one causing UI to go very slow. Thanks.It's not a great idea to attempt to add bulk items into an ObservableCollection. That's not what it's designed to do - and will go slow because it raises a CollectionChanged event on every add. One option would be to derive a custom observable collection which suppresses this event until you have finished adding the data. Off the top of my head, this should work:
public class SuperObservableCollection<T> : ObservableCollection<T>
{
private bool _suppressNotification = false;protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (!_suppressNotification)
base.OnCollectionChanged(e);
}public void AddRange(IEnumerable<T> list)
{
_suppressNotification = true;foreach (T item in list) { Add(item); } \_suppressNotification = false; OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}Deja View - the feeling that you've seen this post before.
-
It's not a great idea to attempt to add bulk items into an ObservableCollection. That's not what it's designed to do - and will go slow because it raises a CollectionChanged event on every add. One option would be to derive a custom observable collection which suppresses this event until you have finished adding the data. Off the top of my head, this should work:
public class SuperObservableCollection<T> : ObservableCollection<T>
{
private bool _suppressNotification = false;protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (!_suppressNotification)
base.OnCollectionChanged(e);
}public void AddRange(IEnumerable<T> list)
{
_suppressNotification = true;foreach (T item in list) { Add(item); } \_suppressNotification = false; OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}Deja View - the feeling that you've seen this post before.
Works for me - thanks
-
Works for me - thanks
Cool, and no problem.
Deja View - the feeling that you've seen this post before.
-
Cool, and no problem.
Deja View - the feeling that you've seen this post before.
Using the Ants profiler it is (adding 1600 objects) roughly 3 times faster
-
It's not a great idea to attempt to add bulk items into an ObservableCollection. That's not what it's designed to do - and will go slow because it raises a CollectionChanged event on every add. One option would be to derive a custom observable collection which suppresses this event until you have finished adding the data. Off the top of my head, this should work:
public class SuperObservableCollection<T> : ObservableCollection<T>
{
private bool _suppressNotification = false;protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (!_suppressNotification)
base.OnCollectionChanged(e);
}public void AddRange(IEnumerable<T> list)
{
_suppressNotification = true;foreach (T item in list) { Add(item); } \_suppressNotification = false; OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}Deja View - the feeling that you've seen this post before.
-
This is exactly what i'm looking for. Tried to do this before but doesnot take in mind about
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
Thanks a lot.You're welcome, and I'm glad I could help out.
Deja View - the feeling that you've seen this post before.
-
Using the Ants profiler it is (adding 1600 objects) roughly 3 times faster
I knew it would be faster, but that's quite a shocking amount.
Deja View - the feeling that you've seen this post before.
-
I knew it would be faster, but that's quite a shocking amount.
Deja View - the feeling that you've seen this post before.
It was only a spot test - I ran it once using Add then again using your changes with AddRange - if it scales then it's a huge improvement but a few more profile runs would be needed to come to any conclusions. Good effort though :-D