Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
System.Windows.Data
 CurrentItem Property
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework Class Library
CollectionView..::.CurrentItem Property

Gets the current item in the view.

Namespace:  System.Windows.Data
Assembly:  PresentationFramework (in PresentationFramework.dll)
Visual Basic
Public Overridable ReadOnly Property CurrentItem As Object
    Get
C#
public virtual Object CurrentItem { get; }
Visual C++
public:
virtual property Object^ CurrentItem {
    Object^ get ();
}
F#
abstract CurrentItem : Object
override CurrentItem : Object

Property Value

Type: System..::.Object
The current item of the view. By default, the first item of the collection starts as the current item.

Implements

ICollectionView..::.CurrentItem

Collection views support the concept of a current record pointer. As you navigate through the objects in a collection view, you are moving a record pointer that allows you to retrieve the object that exists at that particular location in the collection.

Note that moving the current record pointer has some interactions with any sorting or filtering that is applied to the collection. Sorting preserves the current record pointer on the last record selected, but the collection view is restructured around it. (Perhaps the selected record was at the beginning of the list before, but now the selected record might be somewhere in the middle.) Filtering preserves the selected record if that selection remains in view after the filtering. Otherwise, the current record pointer is set to the first record of the filtered collection view.

The current item of the collection is bound to automatically if the target of a binding is a singleton value. If the target is an ItemsControl, the current item is synchronized with the selected item. For example, if a list box is bound to a collection, the CurrentItem is synchronized with the currently selected item.

Notes to Inheritors

Only classes that pass currency handling calls to another internal CollectionView object should override this property; all other derived classes should use the SetCurrent method to update the current values stored in the base class.

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.

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.

Visual Basic
    '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
C#
//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 
      }
  }

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker