Reputation: 955
I have a slider like below:
<Slider Minimum="0" Maximum="{Binding TotalNumberOfPositions,Mode=TwoWay}" Value="{Binding CurrentPosition, Mode=TwoWay}" Margin="5" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp" >
<i:InvokeCommandAction Command="{Binding StopSeekPosition, Source={StaticResource ViewModel}}" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding StartSeekPosition, Source={StaticResource ViewModel}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Slider>
The MouseLeftButtonUp
event is working well and command will be triggered. However MouseLeftButtonDown
is not triggered or it does not trigger my StartSeekPostion
command. I have checked command names in MVVM. Everything is normal but it does not work. I really stuck on this. Is it something wrong with my code or is it an issue about slider control?
Upvotes: 0
Views: 103
Reputation: 6136
Neither is anything really wrong with your code nor is this an issue of the Slider
, but when dealing with RoutedEvents
you always have to keep in mind that anywhere along the route some eventHandling code can handle the event (and if further down the tree: before your handler had the chance), mark it as handled, and your handler will not be called.
So, most likely, the Slider control has a handler for MouseLeftButtonDown
that sets eventArgs.Handled = true;
But you can prepare for this: use the method UIElement.AddHandler( routedEvent, handler, handledEventsToo),
either call it in your code-behind and wire up the event and eventHandler there, or write your own trigger (like <MouseLeftButtonDownEventTrigger HandledEventsToo="true">
) with a flag HandledEventsToo
.
Code-Behind
mySlider.AddHandler(UIElement.MouseLeftButtonDownEvent,
new MouseButtonEventHandler( HandleMouseLeftButtonDown ), handledEventsToo: true);
...
private void HandleMouseLeftButtonDown( object sender, MouseButtonEventArgs e ){...}
custom trigger
public class MouseLeftButtonDownEventTrigger : TriggerBase<UIElement>
{
public bool HandledEventsToo { get; set; }
public bool MarkHandled { get; set; }
private readonly MouseButtonEventHandler m_buttonDownHandler;
public MouseLeftButtonDownEventTrigger()
{
m_buttonDownHandler = Invoke;
}
private void Invoke( object sender, MouseButtonEventArgs eventArgs )
{
InvokeActions( null );
if (MarkHandled) eventArgs.Handled = true;
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.AddHandler( UIElement.MouseLeftButtonDownEvent,
m_buttonDownHandler, HandledEventsToo );
}
protected override void OnDetaching()
{
AssociatedObject.RemoveHandler( UIElement.MouseLeftButtonDownEvent,
m_buttonDownHandler );
base.OnDetaching();
}
}
Upvotes: 1