WPF: Slider doesnt raise MouseLeftButtonDown or MouseLeftButtonUp

后端 未结 4 1952
傲寒
傲寒 2021-01-04 01:02

I tried this XAML:



        
相关标签:
4条回答
  • 2021-01-04 01:05

    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.

    0 讨论(0)
  • 2021-01-04 01:22

    Try using LostMouseCapture and GotMouseCapture.

        private void sliderr_LostMouseCapture(object sender, MouseEventArgs e)
    
        private void slider_GotMouseCapture(object sender, MouseEventArgs e)
    

    GotMouseCapture fires when the user begins dragging the slider, and LostMouseCapture when he releases it.

    0 讨论(0)
  • 2021-01-04 01:29

    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.

    0 讨论(0)
  • 2021-01-04 01:29

    I'd like to mention that the Slider doesn't quite swallow the entire MouseDown event. By clicking on a tick mark, you can get notified for the event. The Slider won't handle MouseDown events unless they come from the slider's... slider.

    Basically if you decide to use the

    AddHandler(Slider.MouseLeftButtonDownEvent, ..., true)
    

    version with the ticks turned on, be sure that the event was handled previously. If you don't you'll end up with an edge case where you thought the slider was clicked, but it was really a tick. Registering for the Preview event is even worse - you'll pick up the event anywhere, even on the white-space between ticks.

    0 讨论(0)
提交回复
热议问题