11

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 ?

2

1 Answer 1

16

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.