Search code examples
c#wpflistviewcode-behind

Listview MouseDoubleClickEvent create in behind code


When I have:

<UserControl.Resources>

    <Style x:Key="itemstyle" TargetType="{x:Type ListViewItem}">
        <EventSetter Event="MouseDoubleClick" Handler="HandleDoubleClick" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
    </Style>
</UserControl.Resources>
<Grid>
    <ListView  ItemContainerStyle="itemstyle"  Name="listView1" >
        <ListView.View>
             <GridView>
                  ... etc

Code behind

    protected void HandleDoubleClick(object sender, MouseButtonEventArgs e)
    {
        T item = (T)(((ListViewItem)sender).Content);   
        // item is the row item that was double clicked on                    
    }

Everything works great.


Now I need to do the same thing on code behind. This is what I have worked out:

 public Constructor(){

    listview.AddHandler(
        ListViewItem.MouseDoubleClickEvent, 
        new MouseButtonEventHandler(HandleDoubleClick)
    );
 }


 protected void HandleDoubleClick(object sender, MouseButtonEventArgs e)
 {
        T item = (T)(((ListViewItem)sender).Content);       
        // error cause the sender is the listview                           
 }

that event fires when I double click any part of the listview not just the listviewItem. Also I expect sender to be ListViewItem and it is not. the sender is actually the listview. Things that I have tried:

1) Because the sender is the listview I tried creating the event as:

         listview.AddHandler(
            // I use PreviewMouseDoubleClickEvent instead of MouseDoubleClickEvent because of the way the events bubles
            ListViewItem.PreviewMouseDoubleClickEvent,   
            new MouseButtonEventHandler(HandleDoubleClick)
         );

I get the same error the sender is the listview

2) Instead of doing:

  T item = (T)((ListViewItem)sender).Content;

I do:

  T item = (T)(listview.selectedItem);

the problem with this is that if the user double clicks anything on the listview that is not the row it will return the current selected item

why is my code not working? what am I doing wrong?


Solution

  • Figure it out!! I am sure it should be the same with the double click...

    In xaml I have:

    <ListView IsSynchronizedWithCurrentItem="True" Name="listView" Margin="32,158,66,0" VerticalAlignment="Top">
            <ListView.ItemContainerStyle>                 
                 <Style TargetType="ListViewItem">
                    <EventSetter Event="PreviewMouseUp" Handler="itemClicked"></EventSetter>
                </Style>                 
            </ListView.ItemContainerStyle>            
            <ListView.View>
            ... etc
    

    and I can create the same thing with c# on code behind as:

        EventSetter ev = new EventSetter();
        ev.Event = ListViewItem.PreviewMouseUpEvent;
        ev.Handler = new MouseButtonEventHandler(itemClicked);
    
        Style myStyle = new Style();
        myStyle.TargetType = typeof(ListViewItem);
    
        myStyle.Setters.Add(ev);
    
    
        listView.ItemContainerStyle = myStyle;
    

    ....

    void itemClicked(object sender, MouseButtonEventArgs e)
    {
         // item was licked in listview implement behavior in here
    }