wpf mvvm datagrid loses sort when context recreated
I have a wpf application built with MVVM light that uses entity framework
as the model.
Background:
My view model has a context that I use to populate a datagrid in the view.
I have a "navigation service" I built that changes what view/viewmodels
are displayed. As part of this navigation service I trigger an event that
the viewmodel uses to refresh the record of the datagrid in the viewmodel
before the requested view is displayed to the user.
Problem:
If the user has sorted the datagrid by clicking on a column heading, that
sorting is lost when I refresh the records. I want to maintain the
datagrid sroting when I drop and recreate the context.
Example:
here is the stripped down version of my refresh records function in the
view model:
Context = Nothing
Context = _ModelService.NewContext
InStockCollection = Await
_TrackingService.GetTracking_Stock_AllAsync(Context)
InStockCollectionViewSource.Source = InStockCollection
here is the declaration of the datagird in the paired view:
<DataGrid x:Name="StockDataGrid"
Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="3"
ItemsSource="{Binding
InStockCollectionViewSource.View,
IsAsync=True}"
CanUserAddRows="False"
CanUserDeleteRows="False"
SelectionMode="Single"
SelectedItem="{Binding SelectedInStock,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
VirtualizingStackPanel.IsVirtualizing="True"
RowHeight="49">
QUESTION:
How can I capture the current sorted value of the datagird in the view
model (i am confortable adding sorting descriptioins to the
collectionviewsource in the code, I just cant figure out how to tell what
sort is currently applied)
OR
Or how can I maintain the sort of the datagrid when the context of the
collectionviewsource is dropped and recreated.
thanks in advance
No comments:
Post a Comment