'WPF: ListBox unselecting
- 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?
Solution 1:[1]
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 to SelectionMode.Multiple) and also tracking the SelectionChanged event automatically. Lets show the code:
public class UnselectableListBox : ListBox
{
public UnselectableListBox() : base()
{
SelectionChanged += new SelectionChangedEventHandler((sender, e) =>
{
if (e.AddedItems.Count > 0)
{
var last = e.AddedItems[0];
foreach (var item in new ArrayList(SelectedItems))
if (item != last) SelectedItems.Remove(item);
}
});
}
static UnselectableListBox()
{
SelectionModeProperty.OverrideMetadata(typeof(UnselectableListBox),
new FrameworkPropertyMetadata(SelectionMode.Multiple));
}
}
Then, I only need to replace my XAML with:
<local:UnselectableListBox ... />
No more need to code SelectionChanged for each ListBox on each Window.
Solution 2:[2]
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: Use PreviewLeftMouseDownClick and PreviewLeftMouseUpClick. Store MyListBox.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 set MyListBox.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!
Solution 3:[3]
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.
Solution 4:[4]
I found that Ctrl-CLick is a non-intuitive way to unselect a listbox item and fixed the behavior by simply adding a MouseDown and KeyDown event in my XAML (.net Core 5) and creating event handlers like this:
private void lboxScanList_MouseDown(object sender, MouseButtonEventArgs e) {
// user clicked on a non-item - deselect all
lboxScanList.UnselectAll();
}
private void lboxScanList_KeyDown(object sender, KeyEventArgs e) {
if (e.Key == Key.Escape) {
lboxScanList.UnselectAll();
}
}
This gives you unselection with Esc or a mouse click off of any item but on the listbox which I feel is much more intuitive.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Erick Petrucelli |
| Solution 2 | Frits |
| Solution 3 | codekaizen |
| Solution 4 | Sanford A Staab |
