WPF - prevents ListBox items from being selected
I would like to prevent ListBoxItems from being selected in my ListBox. My DataTemplate has a checkbox and that should be the only thing the user can click or select. How can i do this? Thank!
This is almost a duplicate question. You are actually asking two questions here:
-
Make the style
ListBoxItem
not show highlighting (look elsewhere on SO for this answer) or replaceListBox
withItemsControl
if you don't need other features thatListBox
. -
Bind the CheckCheck IsChecked property to the parent property
ListBoxItem.IsSelected
:<CheckBox IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}" />
When your user tries (un) to check your checkboxes, then the element will become "active" in some way. And the focused style will be applied. As far as I know, there is no way to disable selection (because if you made your checkboxes it won't work), but you can override the focused (or selected) style of the list items.