none
How do I add a row double-click event handler to ListView/GridView?

    Question

  • I have a ListView using a GridView as its view. How to I add a handler to pick up a row double-click on the GridView?

    My code-behind is C#.

    Thanks.

    Tuesday, March 21, 2006 7:39 PM

Answers

  • Two ways:

    1.

    <Style x:Key="itemstyle" TargetType="{x:Type ListViewItem}">

      <EventSetter Event='MouseDoubleClick' Handler='HandleDoubleClick'/> 
     
    </Style>

    <ListView ItemContainerStyle="{StaticResource itemstyle}">

    2.

     ListViewControl.AddHandler(MouseDoubleClick, new RoutedEventHandler(HandleDoubleClick));

     

    Ji

     

    Wednesday, March 22, 2006 1:39 AM

All replies

  • Two ways:

    1.

    <Style x:Key="itemstyle" TargetType="{x:Type ListViewItem}">

      <EventSetter Event='MouseDoubleClick' Handler='HandleDoubleClick'/> 
     
    </Style>

    <ListView ItemContainerStyle="{StaticResource itemstyle}">

    2.

     ListViewControl.AddHandler(MouseDoubleClick, new RoutedEventHandler(HandleDoubleClick));

     

    Ji

     

    Wednesday, March 22, 2006 1:39 AM
  • I tried both your suggestions, but I can't get either one to work. The C#, it says there is no MouseDoubleClick in this Context. and the x:Key tag fails in the XAML as well as the next line, VS 2005 says Handler is not an attibute.

    I found an EventHandler for a single mouse button up event, but I can't find anything about DoubleClick.

    ListView.AddHandler(GridViewRowPresenter.MouseLeftButtonUpEvent, new RoutedEventHandler(ListView_MouseClick));

    Thursday, March 23, 2006 12:12 AM
  • 1. Style should be add in Resources 

    <StackPanel>

     <StackPanel.Resources>

      <Style x:Key="itemstyle" TargetType="{x:Type ListViewItem}">

       <EventSetter Event='MouseDoubleClick' Handler='HandleDoubleClick'/> 
     
      </Style>

     </StackPanel.Resources>

     <ListView ItemContainerStyle="{StaticResource itemstyle}">

    </StackPanel>

    2.

     ListView.AddHandler(Control.MouseDoubleClickEvent, new RoutedEventHandler(HandleDoubleClick));

     

    Ji

    Thursday, March 23, 2006 9:51 AM
  • Ji Zhang MSFT said:

    1. Style should be add in Resources 

    <StackPanel>

     <StackPanel.Resources>

      <Style x:Key="itemstyle" TargetType="{x:Type ListViewItem}">

       <EventSetter Event='MouseDoubleClick' Handler='HandleDoubleClick'/> 
     
      </Style>

     </StackPanel.Resources>

     <ListView ItemContainerStyle="{StaticResource itemstyle}">

    </StackPanel>

    2.

     ListView.AddHandler(Control.MouseDoubleClickEvent, new RoutedEventHandler(HandleDoubleClick));

     

    Ji



    Thanks for this. I'm just having an issue with the column headers -  they seem to be triggering off the event too (when double clicked by accident). I check the sender but it just returns the listview control so i cant exam what exactly has been clicked. Do you have any ideas around this?
    Thursday, November 06, 2008 8:41 AM
  • learning xaml said:

    Thanks for this. I'm just having an issue with the column headers -  they seem to be triggering off the event too (when double clicked by accident). I check the sender but it just returns the listview control so i cant exam what exactly has been clicked. Do you have any ideas around this?

    I had the same problem and found this thread very useful. In answer to your question though, you need to check the OriginalSource property of the routed event args, the sender will always be the ListView.

    The original source can be any element within the list view item's template, hence you need to check up the ancestors until you find the originating ListViewItem.

    Here is some sample code, in my sample I am responding to click events on listviewitems in a ListView called "ActivitiesItemList".

    Hope it helps.

    1 protected ListView ActivitiesItemList { getprivate set; }  
    2  
    3 public override void OnApplyTemplate()  
    4 {  
    5     base.OnApplyTemplate();  
    6       
    7     ActivitiesItemList = GetTemplateChild("ActivitiesItemList"as ListView;  
    8     if (ActivitiesItemList != null)  
    9     {  
    10         ActivitiesItemList.AddHandler(  
    11             Control.MouseDoubleClickEvent,  
    12             new RoutedEventHandler(ActivitiesItemList_DoubleClick));  
    13     }  
    14 }  
    15  
    16 protected void ActivitiesItemList_DoubleClick(object sender, RoutedEventArgs e)  
    17 {  
    18     DependencyObject depObj = e.OriginalSource as DependencyObject;  
    19     if (depObj != null)  
    20     {  
    21         // go up the visual hierarchy until we find the list view item the click came from  
    22         // the click might have been on the grid or column headers so we need to cater for this  
    23         DependencyObject current = depObj;  
    24         while (current != null && current != ActivitiesItemList)  
    25         {  
    26             ListViewItem lvi = current as ListViewItem;  
    27             if (lvi != null)  
    28             {  
    29                 // this is the list view item  
    30                 // do something with it here  
    31  
    32                 // break out of loop  
    33                 return;  
    34             }  
    35             current = VisualTreeHelper.GetParent(current);  
    36         }  
    37     }                         
    38


    Justin

    • Proposed as answer by Lord Zarquon Sunday, September 06, 2009 11:53 AM
    Wednesday, December 17, 2008 11:39 AM
  • How do i navigate to other page, and send the parameter too? 
    ex: Command="NavigationCommands.GoToPage" CommandParameter="page/details.xaml"

    thanks.
    Monday, November 09, 2009 10:09 PM