Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Selector Class
 SelectedIndex Property
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Selector..::.SelectedIndex Property

Gets or sets the index of the first item in the current selection or returns negative one (-1) if the selection is empty. This is a dependency property.

Namespace:  System.Windows.Controls.Primitives
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Visual Basic (Declaration)
<BindableAttribute(True)> _
<LocalizabilityAttribute(LocalizationCategory.NeverLocalize)> _
Public Property SelectedIndex As Integer
Visual Basic (Usage)
Dim instance As Selector
Dim value As Integer

value = instance.SelectedIndex

instance.SelectedIndex = value
C#
[BindableAttribute(true)]
[LocalizabilityAttribute(LocalizationCategory.NeverLocalize)]
public int SelectedIndex { get; set; }
Visual C++
[BindableAttribute(true)]
[LocalizabilityAttribute(LocalizationCategory::NeverLocalize)]
public:
property int SelectedIndex {
    int get ();
    void set (int value);
}
JScript
public function get SelectedIndex () : int
public function set SelectedIndex (value : int)
XAML Attribute Usage
<object SelectedIndex="int" .../>

Property Value

Type: System..::.Int32
The index of first item in the current selection. The default value is negative one (-1).

Identifier field

SelectedIndexProperty

Metadata properties set to true

BindsTwoWayByDefault, Journal

If a Selector supports selecting a single item, the SelectedIndex property returns the index of the selected item. If a Selector supports multiple selections, SelectedIndex returns the index of the item that the user selected first.

Setting SelectedIndex in a Selector that supports multiple selections clears existing selected items and sets the selection to the item specified by the index. SelectedIndex returns -1 if selection is empty.

If you set SelectedIndex to a value less that -1, an ArgumentException is thrown. If you set SelectedIndex to a value equal or greater than the number of child elements, the value is ignored.

The following example creates a ListBox and sets the SelectedIndex property to 1, which selects the second item in the ListBox.

XAML
<ListBox Margin="10,0,0,5" SelectedIndex="1"
  Name="listBox1" VerticalAlignment="Top" Grid.Column="0" Grid.Row="4">
  <ListBoxItem>Item 0</ListBoxItem>
  <ListBoxItem>Item 1</ListBoxItem>
  <ListBoxItem>Item 2</ListBoxItem>
  <ListBoxItem>Item 3</ListBoxItem>
</ListBox>

The following example uses the SelectedIndex property to determine whether the item at index 0 is the first item in the selection.

C#
if (lb.SelectedIndex == 0)
{
    Item.Content = "Index 0";
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker