Matthias Shapiro

I need fewer hobbies

Clicking or DoubleClicking on an Item in a ListView

This is little more than a pointer to a fantastic post by Mike over at Mike’s Code Blog, but I figured it was worth passing along. Mike’s post is focused on finding which item was double clicked, while mine is on determining when the double clicking happened on an item at all.

 I’ve recently come up against a problem in which we were attaching a doubleclick event to our listview, only to discover it fires when we did something like click on the scrollbar quickly. Since we only wanted it to fire when we were double clicking on the listview item, we had to come up with some way of figuring out where in the listview the user had clicked.

Mike’s code made it easy… I’m reproducing our permutation of it here:

First, we put our event pointer in the XAML like so:

<ListView MouseDoubleClick=”ListViewDoubleClick”>

Then we put this in the code behind:

protected void ListView_MouseDoubleClick(object sender, RoutedEventArgs e)
//grab the original element that was doubleclicked on and search from child to parent until
    //you find either a ListViewItem or the top of the tree

    DependencyObject originalSource = (DependencyObject)e.OriginalSource;
while ((originalSource != null) && !(originalSource is ListViewItem)) 
          originalSource =
       //if it didn’t find a ListViewItem anywhere in the hierarch, it’s because the user
      //didn’t click on one. Therefore, if the variable isn’t null, run the code

      if (originalSource != null)
         //code here

That’s it!

This entry was posted in Helpful Links, WPF and tagged , , , , , . Bookmark the permalink.

6 Responses to Clicking or DoubleClicking on an Item in a ListView

  1. Matthias Shapiro

    Yep… I double checked and EventSetters definitely do not work for styles in a resource dictionary.

  2. vince

    Actually, that’s my (2nd) point as well – and why we use my sample code only on those lists that need to handle the DoubleClick. But by attaching it to the style of the ListItem, the handler gets the item clicked – and you don’t have to search for it.

  3. Matthias Shapiro

    Normally, I would agree. However, our case was one in which we had the item styles determining the visuals of all the ListViews in the application. That’s well over 100 ListViews and not all of them needed to respond to DoubleClick events.

    This solution allows us to use standardized styles while being able to create the DoubleClick events on a case-by-case basis.

  4. vince

    gotta agree using a style is so much easier. we use them extensively and have a StyleManager class with static instances:

    // handle the ListItem double click
    Style style = new Style(typeof(ListViewItem), StyleManager.StyleListViewItem);
    style.Setters.Add(new EventSetter(ListViewItem.MouseDoubleClickEvent,
    new MouseButtonEventHandler(OnDoubleClickListViewItem)));
    TableListView.ItemContainerStyle = style;

  5. sbhl

    Removed tags so that it hopefully does not disappear

    Style TargetType=”{x:Type ListViewItem}”
    EventSetter Event=”Control.MouseDoubleClick” Handler=”OnEdit”

  6. sbhl

    Why not use a style and get directly from sender?

    Place in resources, (should use a name on it if several listviews are used.

    private void OnEdit(object sender, RoutedEventArgs e)
    // The sender is the listview item