共用方式為


HOW TO:使用 CheckBox 建立 ListViewItems

本範例說明如何在使用 GridViewListView 控制項中,顯示一欄 CheckBox

範例

若要在 ListView 中建立包含 CheckBox 控制項的欄,請建立包含 CheckBoxDataTemplate。 然後,將 GridViewColumnCellTemplate 設為 DataTemplate

下列範例會顯示包含 CheckBoxDataTemplate。 此範例會將 CheckBoxIsChecked 屬性繫結至包含它之 ListViewItemIsSelected 屬性值。 因此,當選取包含 CheckBoxListViewItem 時,就會核取 CheckBox

<DataTemplate x:Key="FirstCell">
  <StackPanel Orientation="Horizontal">
    <CheckBox IsChecked="{Binding Path=IsSelected, 
      RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/>
  </StackPanel>
</DataTemplate>

下列範例顯示如何建立一欄 CheckBox 控制項。 為設定欄,此範例會將 GridViewColumnCellTemplate 屬性設為 DataTemplate

<GridViewColumn CellTemplate="{StaticResource FirstCell}" 
                Width="30"/>

請參閱

參考

Control

ListView

GridView

概念

ListView 概觀

GridView 概觀

其他資源

ListView HOW TO 主題