2

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

4

1 回答 1

1

所以我没有意识到MVVM轻框架有EventToCommand的passeventargs属性。

在我看来,我为数据网格的排序事件声明了 EventToCommand,如下所示:

            <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">
        ''column definitions go here
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Sorting">
                        <cmd:EventToCommand Command="{Binding DataContext.SortingCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                                            PassEventArgsToCommand="True"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>

            </DataGrid>

所以在我的视图模型中我有这个:

    Private _SortingCommand As RelayCommand(Of DataGridSortingEventArgs)

    Public ReadOnly Property SortingCommand() As RelayCommand(Of DataGridSortingEventArgs)
        Get
            If _SortingCommand Is Nothing Then
                _SortingCommand = New RelayCommand(Of DataGridSortingEventArgs)(AddressOf SortingCommandExecute)
            End If
            Return _SortingCommand
        End Get
    End Property

    Private Sub SortingCommandExecute(e As DataGridSortingEventArgs)

        Console.WriteLine(e.Column.SortMemberPath.ToString)

    End Sub

现在,我可以使用 DataGridSortingEventArgs 来获取列、列排序路径和排序方向,当我重新创建实体框架数据库上下文时,我可以将其重新应用于集合。

于 2013-10-02T02:36:23.987 回答