Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A WPF CheckBox ListBox

0.00/5 (No votes)
11 Oct 2017 4  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here