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.

Published Monday, February 04, 2008 4:49 PM by marianor
Filed under:

Comments

# re: Multiselect ListView with checkboxes in WPF

Monday, February 18, 2008 4:13 AM by Kundan

Hi,

Thanks for your support...I was trying to do thing form last two days..Thanks a lot, good work done...

# re: Multiselect ListView with checkboxes in WPF

Wednesday, July 16, 2008 3:40 PM by prayag

Hey nice post thanks a lot it help me a g8 deal ..

# re: Multiselect ListView with checkboxes in WPF

Wednesday, August 06, 2008 7:19 AM by booma

how get the checkbox in DataTemplate with programing

# re: Multiselect ListView with checkboxes in WPF

Friday, August 15, 2008 4:19 PM by marianor

Why do you need to do that ?

# re: Multiselect ListView with checkboxes in WPF

Thursday, November 27, 2008 5:50 AM by Colin Eberhardt

Just discovered this post, good stuff! - you might be interested to know that I implemented something similar with the WPF DataGrid:

wpfadventures.wordpress.com/.../multiselect-datagrid-with-checkboxes

# re: Multiselect ListView with checkboxes in WPF

Tuesday, May 12, 2009 10:07 AM by Sunil

Hi

Thank you very much i had been trying to do this from past 2 days and finally i found it here .

Thank you very much

# re: Multiselect ListView with checkboxes in WPF

Thursday, June 25, 2009 10:54 AM by GCSL

Thanks a lot i was searching from last 2 days for this...

But i am having listview with items from database.In the following code i am getting error

foreach (string item in checkedListView.SelectedItems)

Unable to cast object of type 'System.Data.DataRowView' to type 'System.String'.

Please suggest how to resolve this

Many Thanks

# re: Multiselect ListView with checkboxes in WPF

Thursday, June 25, 2009 2:22 PM by marianor

You should cast the items as a DataRowView:

foreach (DataRowView item in checkedListView.SelectedItems)

Because each ListViewItem is bound to each DataRowView in your source.

# Multiselect ListView with checkboxes in WPF zhuan

Monday, August 03, 2009 6:10 AM by javaca88

weblogs.asp.net/.../multiselect-listview-with-checkboxes-in-wpf.aspxM...

# re: Multiselect ListView with checkboxes in WPF

Tuesday, September 22, 2009 2:15 AM by Raman

Thanks a lot,

How to bind CheckBox content programmatically.????

Content="{Binding}"

Content="{Binding EmpID}"  

from design ,column names can be set, but i want to change checkBox content dynamically ..

pelase help , i am trying for this from last 3 days..

Thanks...

# re: Multiselect ListView with checkboxes in WPF

Friday, November 06, 2009 9:34 AM by Balaji

Thanks Man you saved me

Leave a Comment

(required) 
(required) 
(optional)
(required)