WPF; How to deselect all my selected items when clicking on an empty space in my ListView
When I have several (or even one) selected items
and I click a simple one click
on an empty space in my ListView
(empty space = not a row), I want to deselect all my selected items.
This is my function to deselect all items:
private void DeselectAllListViewItems()
{
MyListView.SelectedItems.Clear();
}
I am trying to grab the selected index using this function:
private void MyListView_MouseDown(object sender, MouseButtonEventArgs e)
{
if (MyListView.SelectedIndex == -1)
DeselectAllListViewItems();
}
But in case I have multiple selected items (or one ...), the selected index will never be -1. So how can I tell mine mouse click
is in the blank space and not in the position line?
+3
Verint Verint
source
to share
1 answer
The code below works well enough.
private void MyListView_MouseDown(object sender, MouseButtonEventArgs e)
{
HitTestResult r = VisualTreeHelper.HitTest(this, e.GetPosition(this));
if (r.VisualHit.GetType() != typeof(ListBoxItem))
listView1.UnselectAll();
}
WPF Listbox remove selection by clicking on blank space
+6
J3soon
source
to share