Reputation: 35434
I want to catch the event when a listview is left-clicked on an empty space - i.e. clicking on no item within the listview control.
I've search in the Events list of the listview but found none. How can I do this? Please help!
[Edit] What I want to do if I could catch this event: Deselect all items in the listview.
Upvotes: 6
Views: 6267
Reputation: 3863
I found if I had previously single clicked on an item in the listview (and thereby selecting it), then next double clicked on the empty space in the listview the undesired result of having the previously selected item being actioned as if it had been double clicked on (rather then the empty space). To get around this I used the following code (vb.net):
Private Sub ListView1_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles ListView1.MouseLeftButtonDown
ListView1.SelectedIndex = -1
End Sub
with this code in place double clicking on the empty space de-selects any previously selected items and has the desired effect of nothing appearing to happen for the user when they double click in an empty area.
Upvotes: 0
Reputation: 24453
If you attach a handler to the MouseLeftButtonDown event on the ListView it will only fire when areas outside of a ListViewItem are clicked. Any clicks inside the items will be handled by the items themselves to drive the ListView's selection behavior.
You can make changes to the clickable areas by adjusting the Background ({x:Null} is not clickable, anything else is) and Margin of the ListViewItems by setting an ItemContainerStyle on the ListView. Also make sure that you are not using a null Background on the ListView itself (White is the default, Transparent works too).
Upvotes: 5
Reputation: 13582
ListBoxItem
control handles clicks on ListBox. You should either:
PreviewMouseDown
event on ListBoxmyListBox.AddHandler
methodSee How to Attach to MouseDown Event on ListBox for explanation and code examples.
Upvotes: 0