操作說明:使用 CheckBox 建立 ListViewItems

此範例顯示如何在使用 GridView 的 ListView 控制項中顯示 CheckBox 控制項的資料欄。

範例

若要建立包含 ListView 中 CheckBox 控制項的資料行,請建立包含 CheckBox 的 DataTemplate。 然後,將 GridViewColumn 的 CellTemplate 設定為 DataTemplate。

下列範例會顯示包含 CheckBox 的 DataTemplate 方法。 此範例會將 CheckBox 的 IsChecked 屬性繫結至包含 ListViewItem 的 IsSelected 屬性值。 因此,選取包含 CheckBox 的 ListViewItem 時,將會檢查 CheckBox。

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

下列範例顯示如何建立 CheckBox 控制項的資料行。 若要建立資料行,此範例會將 GridViewColumn 的 CellTemplate 屬性設定為 DataTemplate。

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

另請參閱