1

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?

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Chandan Gupta
  • 452
  • 4
  • 15
  • Possible duplicate of [WPF ListView: Attaching a double-click (on an item) event](http://stackoverflow.com/questions/728205/wpf-listview-attaching-a-double-click-on-an-item-event) – kgzdev Jan 30 '17 at 10:57
  • *"Am I missing something here?"* No, this behavior is intended. – Manfred Radlwimmer Jan 30 '17 at 11:00
  • @ManfredRadlwimmer Apologies, I was not intending to be rude here. – Chandan Gupta Jan 30 '17 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*). – Manfred Radlwimmer Jan 30 '17 at 11:24
  • @ManfredRadlwimmer Thanks! but how do we solve this problem? I tried the code snippet provided below. But it wont reflect the behavior as desired. As I DoubleClick it, the the single click event invokes automatically. – Chandan Gupta Jan 30 '17 at 11:26
  • 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). – Manfred Radlwimmer Jan 30 '17 at 11:30
  • **Option A - non-recommended, ugly workaround:** Execute the **Click** action with a delay and only if double click hasn't already executed. This approach would be problematic since some user might have their double-click speed set really slow so this would introduce significant input-lag. – Manfred Radlwimmer Jan 30 '17 at 11:30
  • **Option B - don't execute anything on click** I don't recall ever seeing this problem in a well designed application. Do you have to execute something on click? Usually a single click only selects an item from ListView. – Manfred Radlwimmer Jan 30 '17 at 11:31
  • **Option C - Make both work together:** Can you redesign your actions to do something that isn't mutually exclusive? e.g. Click = Select, View, etc. Double Click = Open, Edit, etc. As mentioned above, while I understand your problem, I've never seen it in a realistic use-case. Usually click and double click shouldn't get in each others way. – Manfred Radlwimmer Jan 30 '17 at 11:33
  • Yes of course, It is required as per document for me to invoke action on click and don't execute it when double clicked. – Chandan Gupta Jan 30 '17 at 11:33
  • @ChandanGupta Can you have that document changed? From a UX point of view it doesn't make a lot of sense. You can't know beforehand if the user is going to click again. What exactly are you doing on click/double click? Depending on what it is, the people at [UX Exchange](https://ux.stackexchange.com/) might be able to provide a better solution or at least some arguments for your boss/client/superior as to why this would be a bad idea. – Manfred Radlwimmer Jan 30 '17 at 11:36
  • Again, I can't recommend this approach (option A), but if there is no getting around it, [here is a similar question](https://stackoverflow.com/questions/971459/wpf-button-single-click-double-click-issue) of someone who tried to do the same thing. And [here](https://stackoverflow.com/questions/970898/double-click-vs-single-click-are-these-mutually-exclusive) is another one. – Manfred Radlwimmer Jan 30 '17 at 11:46

3 Answers3

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
    }
}
Svekke
  • 1,470
  • 1
  • 12
  • 20
  • I think you are missing the point of the question: *"I click or double click, the click event fires up"* – Manfred Radlwimmer Jan 30 '17 at 11:01
  • In this case, it doesnt fire the MouseDown Event. – Chandan Gupta Jan 30 '17 at 11:01
  • @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. – Chandan Gupta Jan 30 '17 at 11:04
  • @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. – Azaz ul Haq Jan 30 '17 at 11:24
  • `Floorlist.MouseDoubleClick += Floorlist_MouseDoubleClick;` `Floorlist.MouseDown += Floorlist_MouseDown;` FloorList is the ListView here. – Chandan Gupta Jan 30 '17 at 11:30
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" ... />
mm8
  • 163,881
  • 10
  • 57
  • 88
0

Please try following code snippet:

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

For details try this link on MSDN

Azaz ul Haq
  • 1,635
  • 2
  • 18
  • 46
  • 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. – Chandan Gupta Jan 30 '17 at 11:10
  • 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. – Azaz ul Haq Jan 30 '17 at 11:41