views:

2413

answers:

4

I tried this XAML:

<Slider Width="250" Height="25" Minimum="0" Maximum="1" MouseLeftButtonDown="slider_MouseLeftButtonDown" MouseLeftButtonUp="slider_MouseLeftButtonUp" />

And this C#:

private void slider_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
sliderMouseDown = true;
}

private void slider_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
sliderMouseDown = false;
}

The sliderMouseDown variable never changes because the MouseLeftButtonDown and MouseLeftButtonUp events are never raised. How can I get this code to work when a user has the left mouse button down on a slider to have a bool value set to true, and when the mouse is up, the bool is set to false?

+3  A: 

Sliders swallow the MouseDown Events (similar to the button).

You can register for the PreviewMouseDown and PreviewMouseUp events which get fired before the slider has a chance to handle them.

Mike Brown
Thank you, I was able to use the IsMouseCapturedWithinChanged event in combination with the PreviewMouseLeftButtonUp events to accomplish my ultimate goal.
Nick
Does anyone know the rationale for this behavior?
PeterAllenWebb
The reason for this behavior is that the button handles the MouseDown event. By default if any control states that they've handled an event, then no one else gets notification unless they have specified they want to see handled events as well.
Mike Brown
+3  A: 

Another way to do it (and possibly better depending on your scenario) is to register an event handler in procedural code like the following:

this.AddHandler
(
    Slider.MouseLeftButtonDownEvent,
    new MouseButtonEventHandler(slider_MouseLeftButtonDown),
    true
);

Please note the true argument. It basically says that you want to receive that event even if it has been marked as handled. Unfortunately, hooking up an event handler like this can only be done from procedural code and not from xaml.

In other words, with this method, you can register an event handler for the normal event (which bubbles) instead of the preview event which tunnels (and therefore occur at different times).

See the Digging Deeper sidebar on page 70 of WPF Unleashed for more info.

cplotts
Using this trick seems like the kind of hack that could make your job (or someone else's) much harder in the future. Is it really a good idea?
PeterAllenWebb
I wouldn't call it a hack. However, that being said, I would try to avoid using it if possible, but if you are between rock and a hard place ...Let me explain. The preview events fire before the regular events. So, using the preview events might not always work in the situation at hand.
cplotts
It is the "only" way that we can make Wpf work within our MFC application. The preview events mess with our messaging within the application.
Jason Stevenson
Thanks for this solution. I had the same problem and your answer solved it.
Samvel Siradeghyan
Glad it helped you!
cplotts
Corey...can't believe I forgot to mention HandledEventsToo, that is the alternetave and one or the other would be desireable based on a given scenario (for instance if you want to short-circuit the event and handle it at the higher level, You can set the event's handled property to true in the preview event (preventing it from being fired in most cases by a lower element).
Mike Brown
A: 

Thanks! I was looking for this since long time!

Freelance Seo India

A: 

Thank you very much.It works perfectly.