Compartilhar via


Como: Create ListViewItems with a CheckBox

This example shows how to display a column of CheckBox controls in a ListView control that uses a GridView.

Exemplo

To create a column that contains CheckBox controls in a ListView, create a DataTemplate that contains a CheckBox. Then set the CellTemplate of a GridViewColumn to the DataTemplate.

The following example shows a DataTemplate that contains a CheckBox. The example binds the IsChecked property of the CheckBox to the IsSelected property value of the ListViewItem that contains it. Therefore, when the ListViewItem that contains the CheckBox is selected, the CheckBox is checked.

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

The following example shows how to create a column of CheckBox controls. To make the column, the example sets the CellTemplate property of the GridViewColumn to the DataTemplate.

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

Consulte também

Referência

Control

ListView

GridView

Conceitos

Visão geral de ListView

GridView Overview

Outros recursos

ListView How-to Topics