tesicg
tesicg

Reputation: 4053

WPF: Mouse double click on Image using interaction

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!");
}

Upvotes: 4

Views: 9557

Answers (2)

Ayush Kumar
Ayush Kumar

Reputation: 111

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

Upvotes: 0

Ihor Konovalenko
Ihor Konovalenko

Reputation: 1407

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.

Upvotes: 7

Related Questions