0

I'd like to handle both MouseLeftButtonUpand MouseDoubleClick events on Image. I used galasoft mvvm light library for EventToCommand. But, MouseDoubleClick is not called. Actually, it is called, but very rare. Why does this happen and how to fix it?

<ContentControl>
    <Image Source = "{Binding Img}" Stretch="Fill" />

    <i:Interaction.Triggers>
        <i:EventTrigger EventName = "MouseLeftButtonUp" >
            < cmd:EventToCommand Command = "{Binding MouseUpCommand}" PassEventArgsToCommand="True" />
        </i:EventTrigger>
        <i:EventTrigger EventName = "MouseDoubleClick" >
            < cmd:EventToCommand Command = "{Binding DoubleClickCommand}" PassEventArgsToCommand="True" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ContentControl>



private ICommand _doubleClickCommand;
public ICommand DoubleClickCommand
{
    get
    {
        if (_doubleClickCommand== null)
        {
            _doubleClickCommand=new RelayCommand<MouseEventArgs>(DoubleClikcFunc);
        }

        return _doubleClickCommand;
    }
}

private ICommand _mouseUpCommand;
public ICommand MouseUpCommand
{
    get
    {
        if (_mouseUpCommand== null)
        {
            _mouseUpCommand= new RelayCommand<MouseEventArgs>(MouseUpFunc);
        }

        return _mouseUpCommand;
    }
}

private void MouseUpFunc(MouseEventArgs e)
{
    Points.Add(e.GetPosition((IInputElement)e.Source));
}

private void DoubleClikcFunc(MouseEventArgs e)
{
    Points.Add(MaskPoints[0]);
}
theateist
  • 13,879
  • 17
  • 69
  • 109

2 Answers2

1

The second click of a double-click is by definition always preceded by a single click so you would better handle the MouseLeftButtonDown event alone and check the ClickCount property of the EventArgs to determine whether there was a double click:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseLeftButtonDown" >
        <cmd:EventToCommand Command = "{Binding MouseUpCommand}" PassEventArgsToCommand="True" />
    </i:EventTrigger>
</i:Interaction.Triggers>

private ICommand _mouseUpCommand;
public ICommand MouseUpCommand
{
    get
    {
        if (_mouseUpCommand == null)
        {
            _mouseUpCommand = new RelayCommand<MouseButtonEventArgs>(MouseUpFunc);
        }

        return _mouseUpCommand;
    }
}

private void MouseUpFunc(MouseButtonEventArgs e)
{
    if (e.ClickCount == 2)
        Debug.Write("double");
    else
        Debug.Write("single");
}

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

How do we separate click and double click on listview in WPF application?

mm8
  • 163,881
  • 10
  • 57
  • 88
  • I tried this too. It never goes to 'Write("double")`. – theateist May 19 '17 at 13:46
  • It should if you click fast enough :) It certainly works for me. Make sure that you don't do anything "slow" when ClickCount < 2 because then the second click doesn't get recorded fast enough. – mm8 May 19 '17 at 13:53
  • I wrote a very simle code as an example. It has nohing but the binding to the `command` and it still doesn't stop at `double`. Can you try it at your pc? – theateist May 19 '17 at 15:30
  • And if I use `MouseButtonEventArgs` instead of `MouseEventArgs` the returned `e` is always `null` since it's not if type `MouseButtonEventArgs`. – theateist May 19 '17 at 15:33
  • You should handle the MouseLeftButtonDown event as per my answer. It certainly accepts a MouseButtonEvent. Did you really copy my sample code? – mm8 May 19 '17 at 17:10
  • My mistake, I changed the event name in the wrong place. – theateist May 19 '17 at 20:09
0

You have to put the triggers between the Image Tags. Now you bound the click events to the UserControl.

Here is a example with the normal WPF InputBindings:

        <Image Source = "{Binding Img}" Stretch="Fill">
            <Image.InputBindings>
                <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding DoubleClickCommand}" />
                <MouseBinding MouseAction="LeftClick" Command="{Binding MouseUpCommand}" />
            </Image.InputBindings>
        </Image>
MadMax
  • 61
  • 4
  • it works the same as my example. The `MouseUpCommand` is always called regardless of double click. – theateist May 19 '17 at 13:48
  • Of course. Read my answer again: The second click of a double-click is by definition *always* preceded by a single click. – mm8 May 19 '17 at 14:05