开发者

C#, WPF: Do work on each element in a list View

I have a WPF app with a listview control. The application basically loads a file into a list view. I then want to iterate through each listview element, highlighting the current row, and performing a certain task.

When the task is done I update another file and a panel with the res开发者_C百科ults and move on to the next listview element.

I've done stuff like this in WinForms before, but I'm quite green when it comes to WPF. I guess my main concern is how to handle the second thread, other than the UI thread. Is the threading model any different in WPF?

Secondly, the program is essentially a loop through an ObservableCollection:

for (int i = 0 ; i < Collection.Count; i++)
{
   HighlightCurrentRowInListView();
   Collection[i].DoWork();
   PostWorkToPanel();

   // Bad loop around code
   if (i == (Collection.Count - 1))
   { i = -1; }
}

If I make a change to the collection (which is an ObservableCollection), how will the loop update? Whats the best iteration method, foreach, .ForEach(x=>x) for this context? The program should keep running until a "STOP" button or something is pressed.


If you need info on the threading model see the respective MSDN page.

Since you have an observable collection you can handle the CollectionChanged event, you can see if items have been added or removed so you can add or remove those from a workqueue that you could just take items from until it's empty (while-loop).

To change UI-Elements from the background thread you need to use the Dispatcher, see the threading model link i gave for help on that topic.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜