Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
715
How to propagate sort order to ListCollectionView?
posted

Hi,

I actually have two questions.  Attached is a sample project to help reproduce my "problem".  The project has one xamDataGrid and two Microsoft WPF DataGrid.  There's a view model exposing an ObservableCollection.  I need to implement navigation logic in the view model, so I have a few buttons bound to Commands in the view model that will move the active item by calling methods on the default ICollectionView of the ObservableCollection.  (e.g. calling ListCollectionView.MoveCurrentToNext()).  All three grids have IsSynchronizedWithCurrentItem set to true.

1.  The xamDataGrid does not seem to pick up the ICollectionView.CurrentItem if it is bound to the ObservableCollection.  Is it because xamDataGrid creates its own internal view via the RecordManager?

Steps to reproduce:

  • Open the sample project.  Update the xamDataGrid to bind to "Foos" property (the ObservableCollection), not "FoosView" (the ICollectionView of the ObservableCollection).
  • Run the project.
  • Click on a row on one of the Microsoft DataGrid.  Notice both Microsoft DataGrid has the selected row highlighted, whereas the xamDataGrid doesn't.

To get around the problem, I expose the ICollectionView of the ObservableCollection from the view model and bind that to the xamDataGrid.  Now the xamDataGrid will also select the current item.

2.  Now that I bind the xamDataGrid to "FoosView" (ListCollectionView of the ObservableCollection), navigating with the Next/Previous buttons seems to work fine.  Sorting the Microsoft DataGrid will update the corresponding xamDataGrid and navigating with the Next/Previous buttons continue to work as expected.  But if I were to sort the xamDataGrid instead, the sorting on the Microsoft DataGrid does not update.

Steps to reproduce:

  • Make sure the xamDataGrid is bound to the "FoosView".
  • Click the Identifier column on the Microsoft DataGrid twice to change the sort into descending order.  Notice both the xamDataGrid and Microsoft DataGrid updates the sort order as expected.
  • Now click the Identifier column on the xamDataGrid to change the sort into ascending order.  Notice now the xamDataGrid and the Microsoft DataGrid sort order is no longer in sync.  

Is there any way I can get xamDataGrid to update the underlying ListCollectionView when sorting happens?

What I need to do is to implement Next/Previous in the view model, where Next will move the active item "down" based on the display order.  But because xamDataGrid does not seem to update the ListCollectionView, as soon as I change the sort order, Next will move the active item "up".  I thought about using DataPresenterCommands.RecordNext, but it didn't work for me because RecordNext will clear the selected items, whereas I need to keep the selected items even when active item is moved.  Plus, I don't really know how to use the DataPresenterCommands from the view model because they are RoutedCommands.

Thanks in advance for any help!

Harold

WpfApplication2.zip