Selector.IsSelected 附加屬性


取得或設定值,這個值表示是否項目已選取。Gets or sets a value that indicates whether an item is selected.

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


下列範例會建立包含兩個 CheckBox 控制項的 ListBoxThe follow example creates a ListBox that contains two CheckBox controls. 此範例不會明確建立任一 CheckBoxListBoxItem,因此它會在每個 CheckBox上直接設定 Selector.IsSelectedThe example does not explicitly create a ListBoxItem for either CheckBox, so it sets Selector.IsSelected directly on each CheckBox. 為了說明不會更新 Selector.IsSelected,此範例會將第一個核取方塊的 IsChecked 屬性系結至 Selector.IsSelectedTo illustrate that Selector.IsSelected is not updated, the example binds the IsChecked property of the first checkbox to Selector.IsSelected. 當使用者取消選取 ListBox中的專案時,CheckBox 仍會保持核取狀態。When the user unselects the item in the ListBox, the CheckBox remains checked. 同樣地,檢查或取消核取 CheckBox 並不會影響 ListBox中是否已選取 CheckBoxLikewise, checking or unchecking the CheckBox does not affect whether the CheckBox is selected in the ListBox. 此範例會將第二個 CheckBoxIsChecked 系結至 ListBoxItem.IsSelectedThe example binds IsChecked of the second CheckBox to ListBoxItem.IsSelected. 在此情況下,取消選取 [ListBox] 中的專案會使核取方塊成為未核取狀態,而取消核取此核取方塊也會清除該專案。In this case, unselecting the item in the ListBox causes the checkbox to be unchecked, and unchecking the checkbox also unselects the item.

<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

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


當您未明確建立專案容器時,請使用 IsSelected 附加屬性來選取 Selector 中的專案。Use the IsSelected attached property to select an item in the Selector when you do not explicitly create the item container. 專案容器是類別,代表 Selector中的專案。An item container is a class that represents an item in the Selector. TabItemListBoxItemComboBoxItem 是繼承自 Selector之控制項的專案容器,而每個都定義IsSelected屬性。TabItem, ListBoxItem, and ComboBoxItem are item containers for the controls that inherit from Selector, and each defines an IsSelected property.

如果您未明確建立專案容器,Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 會以隱含方式建立一個,並將專案容器上的IsSelected屬性設定為 Selector.IsSelected的值。If you do not explicitly create an item container, Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) implicitly creates one and sets the IsSelected property on the item container to the value of Selector.IsSelected. 這是唯一 Selector.IsSelected 讀取的時間。This is the only time Selector.IsSelected is read. 如果專案上的選取範圍變更,Selector.IsSelected 不會更新。Selector.IsSelected is not updated if the selection on the item changes.

如果您明確建立專案容器,您應該使用該類別上的IsSelected屬性。If you explicitly create an item container, you should use the IsSelected property on that class.

相依性屬性資訊Dependency Property Information

識別碼欄位Identifier field IsSelectedProperty,註冊為附加的IsSelectedProperty, registered as Attached
設定為 true 的中繼資料屬性Metadata properties set to true BindsTwoWayByDefault