Click here to Skip to main content
14,243,097 members

A WPF CheckBox ListBox

Rate this:
5.00 (9 votes)
Please Sign up or sign in to vote.
5.00 (9 votes)
11 Oct 2017CPOL
This tip presents a way to display checkboxes for selection of ListBox Items instead of the default highlighting

Introduction

Many people prefer to see checkboxes instead of the highlighting in a list box. There is a way to do this in XAML that does not require any change in ItemsSource, such as a property to bind to the IsChecked property of the Checkbox.

Design

The following is an example of the XAML that will replace the highlighting of ListBoxItem with a CheckBox:

        <ListBox Margin="15"
                 VerticalAlignment="Stretch"
                 ItemsSource="{Binding Items}"
                 SelectionMode="Multiple">
            <ListBox.Resources>
                <Style TargetType="ListBoxItem">
                    <Setter Property="OverridesDefaultStyle" Value="true" />
                    <Setter Property="SnapsToDevicePixels" Value="true" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <CheckBox Margin="5,2"
                                          IsChecked="{TemplateBinding IsSelected}">
                                    <ContentPresenter />
                                </CheckBox>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.Resources>
        </ListBox>

It requires the replacement of the ListBoxItem Template with a simple Template where the ContentPresenter is just contained in a CheckBox which has its IsChecked property bound to the ListBoxItem IsSelectedProperty. One of the things that this Template does is remove the Triggers that do the highlighting. Instead the highlighting depends on the Triggers for the CheckBox.

History

  • 10/11/2017: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Clifford Nelson
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at qck1@hotmail.com.

Comments and Discussions

 
BugDoes not update SelectedItems Pin
Member 17506285-Oct-18 6:13
memberMember 17506285-Oct-18 6:13 
QuestionThanks for the great article...but this code worked better for me Pin
Kevin Sherrill17-Apr-18 9:47
memberKevin Sherrill17-Apr-18 9:47 
QuestionContentProvider Pin
George Swan13-Oct-17 21:16
memberGeorge Swan13-Oct-17 21:16 
AnswerRe: ContentProvider Pin
Clifford Nelson16-Oct-17 2:51
mvaClifford Nelson16-Oct-17 2:51 
GeneralRe: ContentProvider Pin
George Swan16-Oct-17 4:49
memberGeorge Swan16-Oct-17 4:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 11 Oct 2017

Tagged as

Stats

17.1K views
1.1K downloads
16 bookmarked