Редагувати

Поділитися через


Selector.SelectedIndex Property

Definition

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

public:
 property int SelectedIndex { int get(); void set(int value); };
[System.ComponentModel.Bindable(true)]
[System.Windows.Localizability(System.Windows.LocalizationCategory.NeverLocalize)]
public int SelectedIndex { get; set; }
[<System.ComponentModel.Bindable(true)>]
[<System.Windows.Localizability(System.Windows.LocalizationCategory.NeverLocalize)>]
member this.SelectedIndex : int with get, set
Public Property SelectedIndex As Integer

Property Value

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

Attributes

Examples

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

<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.

if (lb.SelectedIndex == 0)
{
    Item.Content = "Index 0";
}
If lb.SelectedIndex = 0 Then
    Item.Content = "Index 0"

Remarks

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.

Dependency Property Information

Identifier field SelectedIndexProperty
Metadata properties set to true BindsTwoWayByDefault, Journal

Applies to