Multithreading with the WPF DataGrid?
I curr开发者_StackOverflowently have a WPF DataGrid binded to a DataSet via the DataGrid's ItemsSource property in my program. With my current setup, I am having load-time issues that cause the GUI to lock up. Is it possible to multithread the loading of the DataGrid so that it will populate the rows as they are loaded instead of loading all the rows and then populating the DataGrid as it is currently?
I'm very new to the concept of multithreading, so any help will be appreciated!
The problem is to access the UI controls you need be be on the UI thread, so for data binding it's difficult to simply do the work in a separate thread which is otherwise relatively easy. In fact this case requires a bit of trickery.
There's a really good example here which shows how to accomplish this using data virtualization:
http://www.codeproject.com/KB/WPF/WpfDataVirtualization.aspx
This solution makes use of the fact that when an ItemsControl is bound to an IList implementation, rather than an IEnumerable implementation, it will not enumerate the entire list, and instead only accesses the items required for display. It uses the Count property to determine the size of the collection, presumably to set the scroll extents. It will then iterate through the onscreen items using the list indexer. Thus, it is possible to create an IList that can report to have a large number of items, and yet only actually retrieve the items when required.
精彩评论