Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF; How Deselect all my selected items when click on empty space in my ListView

Tags:

listview

wpf

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 ?

like image 337
Verint Verint Avatar asked Jun 09 '15 15:06

Verint Verint


1 Answers

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

like image 103
J3soon Avatar answered Sep 29 '22 10:09

J3soon