Selector.IsSelected 附加屬性

定義

取得或設定值,這個值表示是否項目已選取。

see GetIsSelected, and SetIsSelected
see GetIsSelected, and SetIsSelected
see GetIsSelected, and SetIsSelected

範例

下列範例會建立包含兩 CheckBoxListBox 控制項的 。 此範例不會針對任一 CheckBoxListBoxItem 明確建立 ,因此它會直接在每個 上 CheckBox 設定 Selector.IsSelected 。 為了說明 Selector.IsSelected 未更新,此範例會將第一個核取方塊的 屬性系結 IsCheckedSelector.IsSelected 。 當使用者取消選取 中的 ListBox 專案時, CheckBox 會保持核取狀態。 同樣地,檢查或取消核取 CheckBox 不會影響 CheckBox 是否在 中 ListBox 選取 。 第二 CheckBox 個 的範例系 IsChecked 結至 ListBoxItem.IsSelected 。 在此情況下,取消選取 中的 ListBox 專案會導致核取方塊取消核取,而取消核取核取方塊也會取消選取專案。

<ListBox Name="listBox1" SelectionMode="Multiple">
  <CheckBox Selector.IsSelected="True" 
            IsChecked="{Binding RelativeSource={RelativeSource Self}, 
                        Path=(Selector.IsSelected), Mode=TwoWay}">
    CheckBox.IsChecked is bound to Selector.IsChecked, which is set only
    once.
  </CheckBox>

  <CheckBox ListBox.IsSelected="True" 
            IsChecked="{Binding RelativeSource={RelativeSource 
                        AncestorType={x:Type ListBoxItem}}, 
                        Path=IsSelected, Mode=TwoWay}">
    CheckBox.IsChecked is bound to ListBoxItem.IsChecked, which is
    updated throughout the duration of the application.
  </CheckBox>
</ListBox>

備註

當您未明確建立專案容器時, IsSelected 請使用附加屬性來選取 中的 Selector 專案。 專案容器是類別,表示 中的 Selector 專案。 TabItemListBoxItemComboBoxItem 是繼承自 Selector 之控制項的專案容器,每個控制項都會定義 IsSelected 屬性。

如果您未明確建立專案容器,Windows Presentation Foundation (WPF) 隱含地建立一個容器,並將專案容器上的IsSelected屬性設定為 的值 Selector.IsSelected 。 這是讀取的唯一時間 Selector.IsSelectedSelector.IsSelected 如果專案上的選取專案變更,則不會更新。

如果您明確建立專案容器,您應該在該類別上使用 IsSelected 屬性。

相依性屬性資訊

識別碼欄位 IsSelectedProperty,註冊為附加
設定為 的中繼資料屬性 true BindsTwoWayByDefault

適用於