开发者

How do you read data from an ObservableCollection in C#

开发者 https://www.devze.com 2023-03-12 15:41 出处:网络
It seems Microsoft has oversimplified the databinding aspects of WPF and Silverlight.I 开发者_运维问答have read some data into a couple of \"ObservableCollection\" types but I do not have the need to

It seems Microsoft has oversimplified the databinding aspects of WPF and Silverlight. I 开发者_运维问答have read some data into a couple of "ObservableCollection" types but I do not have the need to bind it to any objects defined in my XAML code. Instead, now that I have read the data, I want to examine it and put it into my own structure.

Searching online, I have not seen any way to easily step through the ObservableCollection types line by line in my code. But there must be a way to do it. So, how do I do it?


into a couple of "ObservableCollection" types but I do not have the need to bind it

If you don't have to DataBind it then there are other (simpler, more genral) Collection classes at your disposal.

But since ObservableCollection<T> : Collection<T>, ... it is quite usable by itself. It supports foreach() and LINQ.

What specific problems are you having?


Are you trying to actually use the Observable Collection (an offshoot of reactive extensions) or do you just need to get data back and enumerate it? This is an important question, as observable and enumerable are different ways of handling the problem.

If you want a better understanding, i suggest you examine some of the videos on reactive extensions. The introductory ones by Erik Meijer are an awesome place to start. NOTE: You don't have to do this to program an observable collection, but understanding the underlying methodology is priceless, IMO.


It is by using the method "ElementAt"

like this, for example. Suppose each one of your obervable collections have a Facility and key:

        string test1 = DataCollection.ElementAt(0).Facility;
        string test2 = DataCollection.ElementAt(0).Key;

This will give you the Facility and the Key of the first element.

0

精彩评论

暂无评论...
验证码 换一张
取 消