How to: Navigate Through the Objects in a Data CollectionView
Views allow the same data collection to be viewed in different ways, depending on sorting, filtering, or grouping. Views also provide a current record pointer concept and enable moving the pointer. This example shows how to get the current object as well as navigate through the objects in a data collection using the functionality provided in the CollectionView class.
Example
In this example, myCollectionView is a CollectionView object that is a view over a bound collection.
In the following example, OnButton is an event handler for the Previous and Next buttons in an application, which are buttons that allow the user to navigate the data collection. Note that the IsCurrentBeforeFirst and IsCurrentAfterLast properties report whether the current record pointer has come to the beginning and the end of the list respectively so that MoveCurrentToFirst and MoveCurrentToLast can be called as appropriately.
The CurrentItem property of the view is cast as an Order to return the current order item in the collection.
'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
//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
}
}
See Also
Tasks
How to: Sort and Group Data Using a View in XAML