A WPF CheckBox ListBox





5.00/5 (13 votes)
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