0

I have a WPF application. There is a listview in which a every time I click or double click, the click event fires up. Even if I keep the Click Event, it automatically fires up when I Double click it. And if I bind the action in DoubleClick, it won't work in single click.

How can I handle both separately?

12
  • Possible duplicate of WPF ListView: Attaching a double-click (on an item) event Commented Jan 30, 2017 at 10:57
  • "Am I missing something here?" No, this behavior is intended. Commented Jan 30, 2017 at 11:00
  • @ManfredRadlwimmer Apologies, I was not intending to be rude here. Commented Jan 30, 2017 at 11:17
  • @ChandanGupta ^^ I wasn't talking about your behavior, there was nothing rude about it. I was talking about the Click event firing even when you double click - that is the intended behavior of the events (intended as in by design). Commented Jan 30, 2017 at 11:24
  • 1
    @ChandanGupta Forget all the answers below, they are missing the point (not handling a click until you are sure it's not going to be a double click). The way I see it, you have a couple of options (see next comments, I'm running out of space here). Commented Jan 30, 2017 at 11:30

3 Answers 3

1

Add a handler to your control:

<SomeControl  MouseDown="MyMouseHandler">
...
</SomeControl>

The handler code:

private void MyMouseHandler(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount == 2)
    {
        //Handle here
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I think you are missing the point of the question: "I click or double click, the click event fires up"
In this case, it doesnt fire the MouseDown Event.
@ManfredRadlwimmer yes it is. Thanks for getting my point. But the thing is if i use MouseDown Event, it doesnt fire up but PreviewMouseDown does fire up both events.
@ChandanGupta, I assume, you are handling both events - MouseDown and PreviewMouseDown. And MouseDown doesn't fire up after PreviewMouseDown. Can you please share the PreviewMouseDown handler code? I suspect somewhere in PreviewMouseDown handler, you have marked event as handled which prevents MouseDown to get fired.
Floorlist.MouseDoubleClick += Floorlist_MouseDoubleClick; Floorlist.MouseDown += Floorlist_MouseDown; FloorList is the ListView here.
1

The second click of a double-click is by definition always preceded by a single click.

If you don't want to handle it you could use a timer to wait for like 200 ms to see if there is another click before you actually handle the event:

public partial class MainWindow : Window
{
    System.Windows.Threading.DispatcherTimer _timer = new System.Windows.Threading.DispatcherTimer();
    public MainWindow()
    {
        InitializeComponent();
        _timer.Interval = TimeSpan.FromSeconds(0.2); //wait for the other click for 200ms
        _timer.Tick += _timer_Tick;
    }

    private void lv_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if(e.ClickCount == 2)
        {
            _timer.Stop();
            System.Diagnostics.Debug.WriteLine("double click"); //handle the double click event here...
        }
        else
        {
            _timer.Start();
        }
    }

    private void _timer_Tick(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("click"); //handle the Click event here...
        _timer.Stop();
    }
}

<ListView PreviewMouseLeftButtonDown="lv_PreviewMouseLeftButtonDown" ... />

Comments

0

Please try following code snippet:

     if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2) {
        // your logic here
    }

For details try this link on MSDN

2 Comments

I tried, but this doesnot work in ListView. I have a databinding in a listview. Also, I never get the Clickcount value moew than 1 event if i bind the action in MouseDown or MouseDoubleClick events.
If you are handling MouseDown event then you should take ClickCount into account. However, if you are handling MouseDoubleClick event then (by definition) it should be handled only on double click as per your requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.