wpf 将对象添加到 ObservableCollection 列表对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15267585/
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-09-13 07:55:47  来源:igfitidea点击:

Add object to ObservableCollection List object

c#wpflistobservablecollection

提问by cp100

How to add one object to ObservableCollection list object? I have class called "Assest" and I have created ObservableCollection list of Asset and I want to maintain it like adding and deleting element from that ObservableCollection list. Now I'm getting error when I try to add single element to ObservableCollection.

如何将一个对象添加到 ObservableCollection 列表对象?我有一个名为“Assest”的类,我创建了资产的 ObservableCollection 列表,我想维护它,就像从该 ObservableCollection 列表中添加和删除元素一样。现在,当我尝试将单个元素添加到 ObservableCollection 时出现错误。

Here's my code.

这是我的代码。

    private static ObservableCollection<Assest> _collection = null;

    public ObservableCollection<Assest> AssestList
    {
        get
        {
            if (_collection == null)
            {
                _collection = new ObservableCollection<Assest>();
            }
            return _collection;
        }
        set { _collection = value; }
    }

    public static ObservableCollection<Assest> ToObservableCollection(List<Assest> assestList)
    {
        return new ObservableCollection<Assest>(assestList);
    }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        LoadData();
        comboBox1.ItemsSource = AssestList;
    }

    private void LoadData()
    {
        Assest assest = new Assest() { AppID = "1", AssestName = "AppName", AppDescription = "Description" };
        Assest assest2 = new Assest { AppDescription = "Des2", AppID = "2", AssestName = "hi" };

        List<Assest> assList = new List<Assest> {assest, assest2};

        ObservableCollection<Assest> generatedAssestList = ToObservableCollection(assList);
        AssestList = generatedAssestList;
    }

    // Here I get an error.

    public static void AddAppToObservalCollection(Assest ass)
    {
        _collection.Add(ass);
    }

So How to over come these kind of situations. Thanks everyone.

那么如何克服这些情况。谢谢大家。

回答by sim1

Your code is a bit messy, it's not clear why you need both AssestList and _collection.

你的代码有点乱,不清楚为什么你需要 AssestList 和 _collection。

However, I think you need to replace

但是,我认为您需要更换

_collection.Add(ass);

with

AssestList.Add(ass);

回答by Xaruth

_collection object still null while you call the getter of AssestList. So, when you use "_collection.Add(ass);", it can be null (and, btw _collection is private, so you can't access it from static function)

当您调用 AssestList 的 getter 时,_collection 对象仍然为 null。因此,当您使用“_collection.Add(ass);”时,它可以为空(并且,顺便说一句 _collection 是私有的,因此您无法从静态函数访问它)

To avoid this, use always AssestList.

为避免这种情况,请始终使用 AssestList。