【问题标题】:wpf mvvm datagrid loses sort when context recreatedwpf mvvm datagrid 在重新创建上下文时丢失排序
【发布时间】:2013-10-08 03:43:40
【问题描述】:

我有一个使用 MVVM light 构建的 wpf 应用程序,它使用实体框架作为模型。

背景:

我的视图模型有一个上下文,用于在视图中填充数据网格。我构建了一个“导航服务”,可以更改显示的视图/视图模型。作为此导航服务的一部分,我触发了一个事件,视图模型用于在向用户显示请求的视图之前刷新视图模型中的数据网格记录。

问题:

如果用户通过单击列标题对数据网格进行了排序,则在我刷新记录时排序会丢失。我想在删除并重新创建上下文时保持数据网格的排序。

示例:

这是视图模型中我的刷新记录功能的精简版:

        Context = Nothing
        Context = _ModelService.NewContext

        InStockCollection = Await _TrackingService.GetTracking_Stock_AllAsync(Context)
        InStockCollectionViewSource.Source = InStockCollection

这是配对视图中数据网格的声明:

            <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">

问题:

如何在视图模型中捕获数据网格的当前排序值(我很乐意在代码中向 collectionviewsource 添加排序描述,我只是不知道如何判断当前应用的排序)

或者当collectionviewsource的上下文被删除并重新创建时,我如何维护数据网格的排序。

提前致谢

【问题讨论】:

    标签: .net wpf entity-framework mvvm datagrid


    【解决方案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 来获取列、列排序路径和排序方向,当我重新创建实体框架数据库上下文时,我可以将它们重新应用于集合。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 2013-04-09
      • 2010-11-17
      • 2010-12-24
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      相关资源
      最近更新 更多