Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert an item at the beginning of an ObservableCollection?

Tags:

c#

How can I do that? I need a list (of type ObservableCollection) where the latest item is first.

like image 965
Jason94 Avatar asked Mar 27 '12 18:03

Jason94


People also ask

How do you use observable collection?

The observable collection works just the same way. If you add or remove something to or from it: someone is notified. And when they are notified, well then they call you and you'll get a ear-full. Of course the consequences are customisable via the event handler.

What is ObservableCollection in C#?

An ObservableCollection is a dynamic collection of objects of a given type. Objects can be added, removed or be updated with an automatic notification of actions. When an object is added to or removed from an observable collection, the UI is automatically updated.

What is ObservableCollection in xamarin forms?

If you want the ListView to automatically update as items are added, removed and changed in the underlying list, you'll need to use an ObservableCollection . ObservableCollection is defined in System.Collections.ObjectModel and is just like List , except that it can notify ListView of any changes: C# Copy.


2 Answers

Try using

collection.Insert(0, item); 

This would add item to the beginning of the collection (while Add adds to the end). More info here.

like image 88
Dmitry Reznik Avatar answered Sep 28 '22 03:09

Dmitry Reznik


You should use a stack instead.

This is based on Observable Stack and Queue

Create an observable Stack, where stack is always last in first out (LIFO).

from Sascha Holl

public class ObservableStack<T> : Stack<T>, INotifyCollectionChanged, INotifyPropertyChanged {     public ObservableStack()     {     }      public ObservableStack(IEnumerable<T> collection)     {         foreach (var item in collection)             base.Push(item);     }      public ObservableStack(List<T> list)     {         foreach (var item in list)             base.Push(item);     }       public new virtual void Clear()     {         base.Clear();         this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));     }      public new virtual T Pop()     {         var item = base.Pop();         this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));         return item;     }      public new virtual void Push(T item)     {         base.Push(item);         this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));     }       public virtual event NotifyCollectionChangedEventHandler CollectionChanged;       protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)     {         this.RaiseCollectionChanged(e);     }      protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)     {         this.RaisePropertyChanged(e);     }       protected virtual event PropertyChangedEventHandler PropertyChanged;       private void RaiseCollectionChanged(NotifyCollectionChangedEventArgs e)     {         if (this.CollectionChanged != null)             this.CollectionChanged(this, e);     }      private void RaisePropertyChanged(PropertyChangedEventArgs e)     {         if (this.PropertyChanged != null)             this.PropertyChanged(this, e);     }       event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged     {         add { this.PropertyChanged += value; }         remove { this.PropertyChanged -= value; }     } } 

This calls INotifyCollectionChanged, does the same as a ObservableCollection, but in a stack manner.

like image 23
Kevin Avatar answered Sep 28 '22 02:09

Kevin