Multiselect ListView with checkboxes in WPF

The main idea to accomplish this is replace the item template in the ListView and the default behavior in the item selection.

The first thing we need is generate an item template containing the checkbox an the text we want to show it. The template is the following:

<DataTemplate x:Key="ItemDataTemplate">

     <CheckBox

        x:Name="checkbox"

        Content="{Binding}"

        IsChecked="{Binding RelativeSource={RelativeSource FindAncestor,

                    AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />

</DataTemplate>

The most important in the template definition is the Binding part of the CheckBox, it searchs for the ListViewItem in the visual tree and synchronize  the checkbox with the IsSelected property in the ListViewItem.

The last part is change the template for the ListViewItem in order to when you select the item do not mark the row as selected, the template is the default for the ListViewItem without triggers:

<ControlTemplate x:Key="ItemTemplate" TargetType="ListViewItem">

      <Border

        BorderThickness="{TemplateBinding Border.BorderThickness}"

        Padding="{TemplateBinding Control.Padding}"

        BorderBrush="{TemplateBinding Border.BorderBrush}"

        Background="{TemplateBinding Panel.Background}"

        SnapsToDevicePixels="True">

            <ContentPresenter

            Content="{TemplateBinding ContentControl.Content}"

            ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"

            HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"

            VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"

            SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />

      </Border>

</ControlTemplate>

 

<Style TargetType="ListViewItem">

      <Setter Property="Template" Value="{StaticResource ItemTemplate}" />

</Style>

It achieve the same behavior of a ListView with checkboxes.

You can download the full sample here.

10 Comments

Comments have been disabled for this content.