How can I do that? I need a list (of type ObservableCollection
) where the latest item is first.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With