Export (0) Print
Expand All

CollectionView.CurrentItem Property

Gets the current item in the view.

Namespace: System.Windows.Data
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public virtual Object CurrentItem { get; }
/** @property */
public Object get_CurrentItem ()

public function get CurrentItem () : Object

You cannot use this property in XAML.

Property Value

The current item of the view. By default, the first item of the collection starts as the current item.

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 now 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 will preserve 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 will be 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.

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.

//OnButton is called whenever the Next or Previous buttons
//are clicked to change the currency
  public 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 
      }
  }

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2014 Microsoft