I have a simple Silverlight 5 datagrid bound to a ObservableCollection of objects:
<sdk:DataGrid x:Name="grid_test" AutoGenerateColumns="False" Grid.Row="1" Height="268" HorizontalAlignment="Left" Margin="16,186,0,0" VerticalAlignment="Top" Width="744">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="Agent Name" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Binding="{Binding agentName}" />
<sdk:DataGridTextColumn Header="Campaign" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Binding="{Binding currentCampaign}" />
<sdk:DataGridTextColumn Header="Currently" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Binding="{Binding currentState}" />
<sdk:DataGridTextColumn Header="Time" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Binding="{Binding displayTime}" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
and in the code:
grid_test.ItemsSource = allKnownAgents;
.. and it works fine, the data is shown and updated automatically, and the user can sort the data by clicking on the column headers. However, when the data changes, the grid doesn’t automatically resort – meaning the sort order isn’t strictly maintained. The user needs to click the column headers again to manually reorder the data.
Is there any easy way to ask/tell Silverlight to maintain the order of data as it’s changing?
One option is instead of an ObservableCollection use a PagedCollectionView. You have much more control over invalidating and re-applying sorting and filtering.