Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!");
}
like image 951
tesicg Avatar asked Oct 26 '25 15:10

tesicg


1 Answers

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.

like image 194
Ihor Konovalenko Avatar answered Oct 29 '25 05:10

Ihor Konovalenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!