Selector.SelectedIndex 屬性

定義

取得或設定目前選取範圍中第一個項目的索引,若選取範圍是空的,則傳回 -1。

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

屬性值

目前選取範圍中第一個項目的索引。 預設值為負 1 (-1)。

屬性

範例

下列範例會建立 , ListBox 並將 SelectedIndex 屬性設定為 1,這會選取 中的 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>

下列範例會 SelectedIndex 使用 屬性來判斷位於索引 0 的專案是否為選取範圍中的第一個專案。

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

備註

Selector如果 支援選取單一專案,屬性會 SelectedIndex 傳回所選取專案的索引。 Selector如果 支援多個選取專案, SelectedIndex 則會傳回使用者第一次選取的專案索引。

在 中 Selector 設定 SelectedIndex ,可清除現有的選取專案,並將選取範圍設定為索引所指定的專案。 SelectedIndex 如果選取範圍是空的,則會傳回 -1。

如果您將 設定 SelectedIndex 為小於 -1 的值, ArgumentException 則會擲回 。 如果您設定 SelectedIndex 為等於或大於子項目數目的值,則會忽略此值。

相依性屬性資訊

識別碼欄位 SelectedIndexProperty
中繼資料屬性設定為 true BindsTwoWayByDefault, Journal

適用於