如何:在数据集合视图中的对象之间导航
视图允许以不同的方式查看相同的数据收集,具体取决于排序、筛选或分组。 视图还提供当前记录指针的概念,并允许移动指针。 此示例演示如何使用 CollectionView 类中提供的功能获取当前对象以及浏览数据收集中的对象。
例
在此示例中,myCollectionView
是一个 CollectionView 对象,该对象是绑定集合的视图。
在以下示例中,OnButton
是应用程序中 Previous
和 Next
按钮的事件处理程序,这些按钮允许用户导航数据收集。 请注意,IsCurrentBeforeFirst 和 IsCurrentAfterLast 属性报告当前记录指针是否分别来到列表的开头和末尾,以便适当地调用 MoveCurrentToFirst 和 MoveCurrentToLast。
视图的 CurrentItem 属性强制转换为 Order
,以返回集合中的当前订单项。
//OnButton is called whenever the Next or Previous buttons
//are clicked to change the currency
private void OnButton(Object sender, RoutedEventArgs args)
{
Button b = sender as Button;
switch (b.Name)
{
case "Previous":
myCollectionView.MoveCurrentToPrevious();
if (myCollectionView.IsCurrentBeforeFirst)
{
myCollectionView.MoveCurrentToLast();
}
break;
case "Next":
myCollectionView.MoveCurrentToNext();
if (myCollectionView.IsCurrentAfterLast)
{
myCollectionView.MoveCurrentToFirst();
}
break;
o = myCollectionView.CurrentItem as Order;
// TODO: do something with the current Order o
}
}
'OnButton is called whenever the Next or Previous buttons
'are clicked to change the currency
Private Sub OnButton(ByVal sender As Object, ByVal args As RoutedEventArgs)
Dim b As Button = TryCast(sender, Button)
Select Case b.Name
Case "Previous"
myCollectionView.MoveCurrentToPrevious()
If myCollectionView.IsCurrentBeforeFirst Then
myCollectionView.MoveCurrentToLast()
End If
Case "Next"
myCollectionView.MoveCurrentToNext()
If myCollectionView.IsCurrentAfterLast Then
myCollectionView.MoveCurrentToFirst()
End If
Exit Select
o = TryCast(myCollectionView.CurrentItem, Order)
' TODO: do something with the current Order o
End Select
End Sub
另请参阅
- 数据绑定概述
- 在视图中对数据进行排序
- 在视图中筛选数据
- 使用 XAML 中的视图
对数据进行排序和分组 - 操作说明主题