How to handle the event when click on empty space?
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 w开发者_运维问答ant to do if I could catch this event: Deselect all items in the listview.
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).
ListBoxItem
control handles clicks on ListBox. You should either:
- use
PreviewMouseDown
event on ListBox - Add event handler in code via
myListBox.AddHandler
method
See How to Attach to MouseDown Event on ListBox for explanation and code examples.
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.
精彩评论