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?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,780 questions
C#
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.
10,948 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 117.6K 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,541 Reputation points Microsoft Vendor
    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 Answers by the question author, which helps users to know the answer solved the author's problem.