Adventures in Silverlight 4 DataGrids
As the initial product cycle on the project I'm currently working spirals toward the end, the bugs I get often seem easy to fix but really are not. This project relies very heavily on the Silverlight DataGrid and RichTextBox (which is new in SL4 and a bit incomplete, a joy for me to code around those holes).
This DataGrid problem manifests itself weirdly: The user loads data into the datagrid, once the rows have been lazily populated from the server, the rows adjust size and now pressing the down arrow doesn't do anything until after a certain amount of presses have happened.
What happened is that the DataGrid calculated that certain rows were visible, say the first 25. After the rows have been populated with data, they almost always grow in size to display more information. Therefore, the visible rows should shrink. In fact, the DataGrid doesn't know the viewable row count has shrunk because it doesn't know to recalculate the viewable area.
The data is lazily gathered because I implemented a basic Data Virtualization strategy for the DataGrid since the row count can be very small or very large and people do not want to wait any long period of time to "see" everything. Paging just wasn't an option (already fought and lost that battle).
The solution is to simply reset the ItemsSource on the DataGrid (first set it to null then back to the collection) which will reset the DisplayData on the DataGrid then use ScrollIntoView to recalculate.
That all seems obvious now but it wasn't when I didn't realize it was only the initial load that was the problem and that I really just wanted to call ScrollIntoView again. By the way, simply calling ScrollIntoView again didn't work because the DataGrid already thought that the row was viewable. It wasn't because the previous rows pushed it out of view but the DataGrid DisplayData didn't get updated.
UI bugs always take the longest.