I have a .NET 4.0 WPF DataGrid with about 2,000 sorted items and one selected item. The DataGrid is updated every 500 ms from a separate thread causing the items to be added and/or removed. After each update the collection of items gets sorted.
The problem is that if the selected item is near the middle of the list, it often goes away from a visible area after several updates. I am looking for a way to update the DataGrid without a user noticing it, i.e. keep the selected item on the same position in the visible area.
So far I have 2 ideas how to work around this problem, but none of them makes me proud:
- Change the updating process from automatic to manual, i.e. put a TextArea with a number of pending updates and an "Update" button. This is not desirable, since I would need to re-negotiate with my product owner.
- Loop through the items before each update to get a collection of visible items (using this approach) and get a relative position of the selected item within the visible items. Then run the updates and scroll the selected item into the initial position using
ScrollIntoView
. The problem with that uproach is that the program will have to loop through all items every 500 ms, which is quite a lot of work for a processor.
I used to solve this problem in WinForms by using TopIndex property, but there is no equivalent of it in WPF.
To sum up, any ideas about how it is possible to add/delete items in a sorted and frequently updated DataGrid without a user having to chase an item of interest will be much appreciated.