tags:

views:

200

answers:

2
  1. When the user clicks on a item in my single-select ListBox, the item is selected.
  2. 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?

+1  A: 

Make sure the selection mode is Multiple. By selecting multiple:

you can use the mouse to select and deselect any item(s) you want with just a mouseclick. But if you want only 1 selected item at a time, you'll have to deselect the other items in code in the SelectionChanged event.

Source

Private Sub MainList_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
    If e.AddedItems.Count > 0 Then
        Dim valid = e.AddedItems(0)
        For Each item In New ArrayList(MainList.SelectedItems)
            If item IsNot valid Then MainList.SelectedItems.Remove(item)
        Next
    End If
End Sub
ChrisF
That was definitely not what I was expecting, but it worked great.
Jonathan Allen
@Jonathan - thanks for adding the code.
ChrisF
A: 

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.

codekaizen
Normally I would agree, but for my current design being able to easily unselect items is very important.
Jonathan Allen
P.S. One of my concerns is that I want mouse-only control. If this gets placed on a touch screen, there will not be a keyboard available.
Jonathan Allen