gpt4 book ai didi

c# - 在 WPF 中向 ObservableCollection 添加数据

转载 作者:太空宇宙 更新时间:2023-11-03 20:39:54 25 4
gpt4 key购买 nike

我这里有一些问题。这里是:我有这门课

public class NewsFeedResources
{
public string Name { get; set; }
public string Id { get; set; }
public string Message { get; set; }

public static ObservableCollection<NewsFeedResources> _newsfeed = new ObservableCollection<NewsFeedResources>
{

new NewsFeedResources { Name = "Joe", Id = "1", Message="Foo" },
new NewsFeedResources { Name = "Wandy", Id = "2", Message="Bar" },
new NewsFeedResources { Name = "Yuliana", Id = "3", Message="Baz" },
new NewsFeedResources { Name = "Hardi", Id = "4", Message="Baz" },

};

public static ObservableCollection<NewsFeedResources> newsFeedResources
{ get { return _newsfeed; }

}
}

如果我有其他数据如

Name=John, Id=5, Message="堆栈溢出"
Name=Jane, Id=6, Message="Hello world"

如何将数据添加到类中,而不是从构造函数中添加?感谢您的帮助

最佳答案

ObservableCollection公开了 Collection<T>.Add 方法:

Adds an object to the end of the Collection.

所以你会:

_newsfeed.Add(new NewsFeedResources {Name = "John",
Id = 5,
Message = "Stack overflow"});
_newsfeed.Add(new NewsFeedResources {Name = "Jane",
Id = 6,
Message = "Hello world"});

(凭内存打字)

关于c# - 在 WPF 中向 ObservableCollection 添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3451858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com