在测试中以编程方式设置分组时如何读取 XamDataGrid 中的分组记录?
How to read grouped records in XamDataGrid when grouping is set programatically in tests?
我正在尝试为 XamDataGrid 编写一些测试方案。在这些情况下,我想在单个字段上设置分组。当我 运行 应用程序并双击网格时,此代码会导致网格按预期分组:
Private Sub MyGrid_MouseDoubleClick(sender As Object, e As MouseButtonEventArgs) Handles MyGrid.MouseDoubleClick
Dim xamDataGrid As XamDataGrid = CType(sender, XamDataGrid)
Dim field As Field = xamDataGrid.FieldLayouts(0).Fields("A")
Dim fieldSortDescription As New FieldSortDescription With {.Field = field, .Direction = ListSortDirection.Ascending, .IsGroupBy = True}
xamDataGrid.FieldLayouts(0).SortedFields.Add(fieldSortDescription)
End Sub
在测试中,运行在 Records 和 ViewableRecords 集合非常相似的情况下不反映分组。 xamDataGrid.ViewableRecords(0)
是 DataRecord 而不是 GroupByRecord。这是测试的代码:
<TestMethod()>
Public Sub Test()
Dim xamDataGrid As New XamDataGrid
xamDataGrid.DataSource = dataSource.DefaultView
xamDataGrid.BeginInit()
xamDataGrid.EndInit()
Dim field As Field = xamDataGrid.FieldLayouts(0).Fields("A")
Dim fieldSortDescription As New FieldSortDescription With {.Field = field, .Direction = ListSortDirection.Ascending, .IsGroupBy = True}
xamDataGrid.FieldLayouts(0).SortedFields.Add(fieldSortDescription)
' exception thrown here because xamDataGrid.ViewableRecords(0) is a DataRecord
Dim groupByRecord As GroupByRecord = CType(xamDataGrid.ViewableRecords(0), GroupByRecord)
' ...
End Sub
如何刷新或以其他方式强制网格反映代码设置的分组?
感谢您的宝贵时间。
我很意外地发现 Records 和 ViewableRecords 集合在测试中使用 Dispatcher 刷新:
<TestMethod()>
Public Sub Test()
Dim xamDataGrid As New XamDataGrid
xamDataGrid.DataSource = dataSource.DefaultView
xamDataGrid.BeginInit()
xamDataGrid.EndInit()
Dim field As Field = xamDataGrid.FieldLayouts(0).Fields("A")
Dim fieldSortDescription As New FieldSortDescription With {.Field = field, .Direction = ListSortDirection.Ascending, .IsGroupBy = True}
xamDataGrid.FieldLayouts(0).SortedFields.Add(fieldSortDescription)
' this line refreshes record collections
Dispatcher.CurrentDispatcher.Invoke(New Action(Sub()
End Sub), DispatcherPriority.ContextIdle)
' exception not thrown!
Dim groupByRecord As GroupByRecord = CType(xamDataGrid.ViewableRecords(0), GroupByRecord)
' ...
End Sub
我正在尝试为 XamDataGrid 编写一些测试方案。在这些情况下,我想在单个字段上设置分组。当我 运行 应用程序并双击网格时,此代码会导致网格按预期分组:
Private Sub MyGrid_MouseDoubleClick(sender As Object, e As MouseButtonEventArgs) Handles MyGrid.MouseDoubleClick Dim xamDataGrid As XamDataGrid = CType(sender, XamDataGrid) Dim field As Field = xamDataGrid.FieldLayouts(0).Fields("A") Dim fieldSortDescription As New FieldSortDescription With {.Field = field, .Direction = ListSortDirection.Ascending, .IsGroupBy = True} xamDataGrid.FieldLayouts(0).SortedFields.Add(fieldSortDescription) End Sub
在测试中,运行在 Records 和 ViewableRecords 集合非常相似的情况下不反映分组。 xamDataGrid.ViewableRecords(0)
是 DataRecord 而不是 GroupByRecord。这是测试的代码:
<TestMethod()> Public Sub Test() Dim xamDataGrid As New XamDataGrid xamDataGrid.DataSource = dataSource.DefaultView xamDataGrid.BeginInit() xamDataGrid.EndInit() Dim field As Field = xamDataGrid.FieldLayouts(0).Fields("A") Dim fieldSortDescription As New FieldSortDescription With {.Field = field, .Direction = ListSortDirection.Ascending, .IsGroupBy = True} xamDataGrid.FieldLayouts(0).SortedFields.Add(fieldSortDescription) ' exception thrown here because xamDataGrid.ViewableRecords(0) is a DataRecord Dim groupByRecord As GroupByRecord = CType(xamDataGrid.ViewableRecords(0), GroupByRecord) ' ... End Sub
如何刷新或以其他方式强制网格反映代码设置的分组?
感谢您的宝贵时间。
我很意外地发现 Records 和 ViewableRecords 集合在测试中使用 Dispatcher 刷新:
<TestMethod()>
Public Sub Test()
Dim xamDataGrid As New XamDataGrid
xamDataGrid.DataSource = dataSource.DefaultView
xamDataGrid.BeginInit()
xamDataGrid.EndInit()
Dim field As Field = xamDataGrid.FieldLayouts(0).Fields("A")
Dim fieldSortDescription As New FieldSortDescription With {.Field = field, .Direction = ListSortDirection.Ascending, .IsGroupBy = True}
xamDataGrid.FieldLayouts(0).SortedFields.Add(fieldSortDescription)
' this line refreshes record collections
Dispatcher.CurrentDispatcher.Invoke(New Action(Sub()
End Sub), DispatcherPriority.ContextIdle)
' exception not thrown!
Dim groupByRecord As GroupByRecord = CType(xamDataGrid.ViewableRecords(0), GroupByRecord)
' ...
End Sub