4

I'm trying to use interaction features to achieve mouse double click on standard Image control. The Image control is on UserControl and method that should handle mouse double click is on view model. The code is as following:

1) UserControl:

<ItemsControl Grid.Row="0" ItemsSource="{Binding SelectedEventPhotoList}"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"                  
            Name="SelectedListView">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="1" Columns="3"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Image Source="{Binding}" Stretch="None">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseDoubleClick">
                        <ei:CallMethodAction MethodName="DblClick" TargetObject="{Binding}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Image>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

3) View model:

public void DblClick()
{
    MessageBox.Show("Double click!");
}

But, it doesn't work.

UPDATE:

I did this, but it doesn't work:

1) XAML:

<ItemsControl Grid.Row="0" ItemsSource="{Binding SelectedEventPhotoList}"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"                  
            Name="SelectedListView">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="1" Columns="3"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
                <Image Source="{Binding}">
                    <Image.InputBindings>
                        <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding MouseDoubleClickCommand}"/>
                    </Image.InputBindings>
                </Image>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

2) View model:

public DelegateCommand MouseDoubleClickCommand { get; private set; }

In constructor:

MouseDoubleClickCommand = new DelegateCommand(DblClick);

And added method:

public void DblClick()
{
    MessageBox.Show("Double click!");
}
tesicg
  • 3,971
  • 16
  • 62
  • 121

3 Answers3

10

The Image class has no MouseDoubleClick event but you could use an InputBinding that binds to an ICommand property:

<Image Source="pic.png">
    <Image.InputBindings>
        <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding MouseDoubleClickCommand}"/>
    </Image.InputBindings>
</Image>

public ICommand MouseDoubleClickCommand { get; } = new DelegateCommand<object>(obj => { MessageBox.Show(""); });

You will need to provide an implementation of the ICommand interface or use someone else's. The DelegateCommand<T> class is available in Prism: https://www.nuget.org/packages/Prism.Wpf/

Please refer to the following blog post for more information about commands and how to handle events using the MVVM design pattern: https://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/

Edit:

If the command is defined in the view model, i.e. the DataContext of the ItemsControl itself, you should use a RelativeSource to be able to bind to it:

<DataTemplate>
    <Image Source="{Binding}">
        <Image.InputBindings>
            <MouseBinding MouseAction="LeftDoubleClick"
                          Command="{Binding DataContext.MouseDoubleClickCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"/>
        </Image.InputBindings>
    </Image>
</DataTemplate>
mm8
  • 163,881
  • 10
  • 57
  • 88
7

Sometimes it is useful to handle OnMouseDownClickCount (or MouseLeftButtonDown) event handler and check ClickCount property of MouseButtonEventArgs argument. This approach allows to handle single-, double-, triple-clicks etc :)

private void OnMouseDownClickCount(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount == 2)
    {
        // Double Click occurred.
        ...
    }
}

But this approach has one "feature". While double-clicking OnMouseDownClickCount event rises two times: first with e.ClickCount == 1 and then with e.ClickCount == 2.

Ihor Konovalenko
  • 1,298
  • 2
  • 16
  • 21
0

Adding on to @mm8 answers if you don't want to download prism, you can create a ICommand class like

namespace Lucid_Notes
{
  public class SimpleCommand : ICommand
  {
    public event EventHandler<object> Executed;

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        if (Executed != null)
            Executed(this, parameter);
    }
    public event EventHandler CanExecuteChanged;
  }
}

And use it as a Command:

<Image Source="sample-image.png">
  <Image.InputBindings>
    <MouseBinding MouseAction="LeftDoubleClick">
        <MouseBinding.Command>
            <local:SimpleCommand Executed="DoubleClickAction" />
      </MouseBinding.Command>
    </MouseBinding>
  </Image.InputBindings>
</Image>

Here, local representing the namespace where the class is present xmlns:local="clr-namespace:Lucid_Notes". Finally add the function you want to execute on double click in the corresponding

  private void DoubleClickAction(object sender, object e)
  {
        Console.WriteLine("test");
  }

Source: https://stackoverflow.com/a/38601721/9794570
Example: https://github.com/ayush1920/Stackoverflow/tree/main/sample-Image-Click

Ayush Kumar
  • 91
  • 1
  • 5