- When the user clicks on a item in my single-select
ListBox
, the item is selected. - When the user clicks on a item the second time, the item is not unselected unless they are holding the control key.
What is the recommended way to change #2 to not require the control key?
Sorry by resurrecting this topic...
But the accepted answer didn't cover well all my needs, so I improved it a little. I have dozens of
ListBox
and it all needs this unselection on click behavior.Because of it, I created a custom control, overriding the
SelectionModeProperty
(setting its default value toSelectionMode.Multiple
) and also tracking theSelectionChanged
event automatically. Lets show the code:Then, I only need to replace my XAML with:
No more need to code
SelectionChanged
for eachListBox
on eachWindow
.Make sure the selection mode is
Multiple
. By selecting multiple:Source
Using the Control key is the recommended way to do this. It's a UI gesture which is consistent across all of Windows. You should be wary of changing this behavior.
Not trying to beat a dead horse but I had this same problem and couldn't use any of the methods used in answers here... hope someone can benefit from my answer:
I have selection mode set to single. I have double click events which makes multiple select mode not so desirable and hard to get double clicks to work.
SelectionChanged
only fires if the selected item is different. Easy way: UsePreviewLeftMouseDownClick
andPreviewLeftMouseUpClick
. StoreMyListBox.SelectedIndex
into an object variable in the preview mouse down event.In the preview mouse up event compare that variable from the down event to
MyListBox.SelectedIndex
and if they are the same setMyListBox.SelectedIndex = -1
.The item will be deselected if it is the same because the changing of index numbers happens after the preview down and before the preview up!