views:

2161

answers:

6

How to get the item double click event of listview?

A: 

In the ListBox DoubleClick event get the selecteditem(s) member of the listbox, and there you are.

void ListBox1DoubleClick(object sender, EventArgs e)
 {
  MessageBox.Show(string.Format("SelectedItem:\n{0}",listBox1.SelectedItem.ToString()));
 }
NemoPeti
If I use the ListBox double click event I can double click anywhere on the listview and if any item selected it will get. I dont need it. I need the clicked item only on when it was doubleclicked.
Sauron
This doesn't work, because you'll catch double clicks on the scroll bar or other awkward places
Paul Betts
A: 

Either use the MouseDoubleClick event, and also, all the MouseClick events have a click count in the eventargs variable 'e'. So if e.ClickCount == 2, then doubleclicked.

Joel
A: 

I'm using something like this to only trigger on ListViewItem double-click and not for example when you double-click on the header of the ListView.

private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    DependencyObject obj = (DependencyObject)e.OriginalSource;

    while (obj != null && obj != myListView)
    {
        if (obj.GetType() == typeof(ListViewItem))
        {
            // Do something here
            MessageBox.Show("A ListViewItem was double clicked!");

            break;
        }
        obj = VisualTreeHelper.GetParent(obj);
    }
}
Oskar
A: 

It's annoying, but the best way to do it is something like:

<DataTemplate Name="MyCoolDataTemplate">
    <Grid Loaded="HookLVIClicked" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}">
        <!-- your code here -->
    </Grid>
</DataTemplate>

Then in the code:

public void HookLVIClicked(object sender, RoutedEventArgs e) {
    var fe = (FrameworkElement)sender;
    var lvi = (ListViewItem)fe.Tag;
    lvi.MouseDoubleClick += MyMouseDoubleClickHandler;
}
Paul Betts
+5  A: 
<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <EventSetter Event="MouseDoubleClick" Handler="listViewItem_MouseDoubleClick" />
    </Style>
</ListView.ItemContainerStyle>

The only difficulty then is if you are interested in the underlying object the listviewitem maps to e.g.

private void listViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    ListViewItem item = sender as ListViewItem;
    object obj = item.Content;
}
Oliver
A: 

I needed that as well. I found that on msdn:

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.activation.aspx

I think this delegate is for that.

AngeDeLaMort