C# 如何在 ObservableCollection 的开头插入一个项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9895394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:07:06  来源:igfitidea点击:

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

c#

提问by Jason94

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

我怎样才能做到这一点?我需要一个列表(类型ObservableCollection),其中最新的项目是第一个。

采纳答案by Dmitry Reznik

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.

这会将 item 添加到集合的开头(而 Add 添加到末尾)。更多信息在这里

回答by Kevin

You should use a stack instead.

您应该改用堆栈。

This is based on Observable Stack and Queue

这是基于Observable Stack 和 Queue

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

创建一个可观察的堆栈,其中堆栈总是后进先出(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.

这将调用 INotifyCollectionChanged,与 ObservableCollection 的作用相同,但采用堆栈方式。

回答by Samet Dumankaya

you can try this

你可以试试这个

collection.insert(0,collection.ElementAt(collection.Count - 1));

collection.insert(0,collection.ElementAt(collection.Count - 1));