如何使用 ObservableCollection 类中的数据?
我正在用 C# 构建一个小程序。我是编程新手,似乎不明白如何做到这一点。 我有一个 ObservableCollection 类,我只能向其中添加数据。
public class Destinations : ObservableCollection<Destination>
{
public Destinations()
: base()
{ Add(new Destination("from", "to", distance, total_distance, "no reason"));
...
Destinations d;
d = new Destinations();
destinations.ItemsSource = d;
d.Add(new Destination(lines[i], lines[i + dim / 2], distance, dist, null));
我如何访问我放入集合中的信息。 我搜索了这些方法,但没有发现任何有用的东西。 我确信这是我的错,但请帮助我。
I am building a small program in C#. I am new to programming and and can't seem to understand how to do this.
I have a ObservableCollection class and I only manage to add data to it.
public class Destinations : ObservableCollection<Destination>
{
public Destinations()
: base()
{ Add(new Destination("from", "to", distance, total_distance, "no reason"));
...
Destinations d;
d = new Destinations();
destinations.ItemsSource = d;
d.Add(new Destination(lines[i], lines[i + dim / 2], distance, dist, null));
How can I access the information I have put in the collection.
I've searched through the methods and found nothing helpful.
I'm sure it's my fault but please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ObservableCollection 有一个 Indexer(也称为 Item 属性)。就像数组或列表一样,您只需提供要检索它的元素的索引......
目的地 d;
d = 新目的地();
d.Add(new Destination(lines[i],lines[i + dim / 2],distance,dist,null));
目的地 d0 = d[0];
http://msdn.microsoft.com/en-us/library/ms668604.aspx
The ObservableCollection has an Indexer (aka Item property). Like an array or a list you just supply the index of the element you want to retrieve it....
Destinations d;
d = new Destinations();
d.Add(new Destination(lines[i], lines[i + dim / 2], distance, dist, null));
Destination d0 = d[0];
http://msdn.microsoft.com/en-us/library/ms668604.aspx
您想检索添加到集合中的项目吗?您可以使用索引器,或者在每个中枚举它,...您遇到的问题是什么?
另外,如果将集合绑定到控件(即列表控件),则集合的元素也应该绑定到该控件。
Do you want to retrieve the items you added to the collection? You can use the indexer for that, or enumerate it in a for each, ... What is the problem which you're having?
Also, if you bind the collection to a control (i.e., a list control), the elements of the collection should be bound to the control as well.