falukky
falukky

Reputation: 1139

WPF: why PreviewMouseLeftButtonDown EventTrigger works and MouseLeftButtonDown not?

Please see this EventTrigger:

<ListView Name="ListViewFiles">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="PreviewMouseLeftButtonDown">
            <i:InvokeCommandAction Command="{Binding ListViewItemMouseLeftButtonDownCommand}"
                                   CommandParameter="{Binding ElementName=ListViewFiles, Path=SelectedItem}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListView>

So i want to achieve 2 things:

  1. I try to use MouseLeftButtonDown instead of PreviewMouseLeftButtonDown but the event is not fired (why ??).
  2. When the user click and hold the left button i want the event the fire after 500 milliseconds currently i am using timer - any other approach ?

Upvotes: 2

Views: 1416

Answers (1)

Keith Stein
Keith Stein

Reputation: 6724

MouseLeftButtonDown probably isn't activating your trigger because of how WPF handles RoutedEvents. When a RoutedEvent gets marked "handled", WPF doesn't call any further event handlers for it.

ListView is almost definitely handling MouseLeftButtonDown internally, probably to change the selected item. Because MouseLeftButtonDown is a bubbling event, that means it gets marked as handled inside the ListView before it gets up the visual tree to your code, so it never arrives.

PreviewMouseLeftButtonDown, on the other hand, is a tunneling event, so it reaches your code before getting into the ListView.

As for the second part of your question, I don't know any better way to trigger code after some time than by using some sort of timer. Also, if you want to trigger it only after they've been holding the button for that amount of time, make sure you handle the PreviewLeftMouseButtonUp event to turn the timer off if they release the button before the time is up.

Upvotes: 2

Related Questions