When i have several (or even one) selected items
and i press simple click
on empty space in my ListView
(empty space = not row) i want to deselect all my selected items.
This is my deselect all item function:
private void DeselectAllListViewItems()
{
MyListView.SelectedItems.Clear();
}
I try to take the selected index with this function:
private void MyListView_MouseDown(object sender, MouseButtonEventArgs e)
{
if (MyListView.SelectedIndex == -1)
DeselectAllListViewItems();
}
But in case i have several selected items (or one..) the selected index will never be -1.
So how can i distinguish that my mouse click
is on empty space and not on item row ?
The code below works quite well.
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 a blank spot
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With