How do I implement a "mouse double click" for a ListBox?

bhs67 96 Reputation points
2024-02-25T13:16:24.31+00:00

This xaml code attempts to implement a "mouse double click" for a ListBox "list of cities". xaml

<ListBox x:Name="gLBxCities" Margin="10,0,10,10" Foreground="#0000FF" >

xaml.cs

private

The Compiler -> The member "Handler" is not recognized or is not accessible. I'm not sure of where Handler should be placed?

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 125.2K Reputation points
    2024-02-25T15:54:59.89+00:00

    This approach seems to work:

    <ListBox x:Name="gLBxCities" . . .>
        <ListBox.Resources>
            <Style TargetType="ListBoxItem">
                <EventSetter Event="MouseDoubleClick" Handler="MyHandler"/>
            </Style>
        </ListBox.Resources>
        
    </ListBox>
    
    private void MyHandler( object sender, MouseButtonEventArgs e )
    {
    
    }
    
    0 comments No comments

  2. Hui Liu-MSFT 48,706 Reputation points Microsoft External Staff
    2024-02-26T02:52:17.61+00:00

    Hi,@bhs67 . Welcome to Microsoft Q&A.
    To handle a "mouse double click" event in a WPF ListBox, you could use the MouseDoubleClick event and define a corresponding event handler in your code-behind (.xaml.cs) file. Here's an example:

    <ListBox x:Name="gLBxCities" Margin="10,0,10,10" Foreground="#0000FF" MouseDoubleClick="gLBxCities_MouseDoubleClick" >
        <ListBoxItem >q</ListBoxItem>
        <ListBoxItem >s</ListBoxItem>
        <ListBoxItem >4</ListBoxItem>
    </ListBox>
    
    
    

    Codebehind:

      private void gLBxCities_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
                     MessageBox.Show("gLBxCities");
        }
    
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.