C# WPF:滑块不会引发 MouseLeftButtonDown 或 MouseLeftButtonUp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/160995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF: Slider doesnt raise MouseLeftButtonDown or MouseLeftButtonUp
提问by Nick
I tried this XAML:
我试过这个 XAML:
<Slider Width="250" Height="25" Minimum="0" Maximum="1" MouseLeftButtonDown="slider_MouseLeftButtonDown" MouseLeftButtonUp="slider_MouseLeftButtonUp" />
And this C#:
而这个 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?
由于永远不会引发 MouseLeftButtonDown 和 MouseLeftButtonUp 事件,因此 sliderMouseDown 变量永远不会改变。当用户在滑块上按下鼠标左键以将 bool 值设置为 true 时,如何使此代码工作,并且当鼠标抬起时,bool 设置为 false?
采纳答案by Michael Brown
Sliders swallow the MouseDown Events (similar to the button).
滑块吞下 MouseDown 事件(类似于按钮)。
You can register for the PreviewMouseDown and PreviewMouseUp events which get fired before the slider has a chance to handle them.
您可以注册 PreviewMouseDown 和 PreviewMouseUp 事件,这些事件在滑块有机会处理它们之前被触发。
回答by cplotts
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.
请注意正确的论点。它基本上表示您希望接收该事件,即使它已被标记为已处理。不幸的是,连接这样的事件处理程序只能通过过程代码完成,而不能通过 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 Unleashedfor more info.
有关详细信息,请参阅WPF Unleashed第 70 页上的“深入挖掘”侧边栏。
回答by benjamin.popp
I'd like to mention that the Slider doesn't quite swallow the entire MouseDown event. By clicking on a tick mark, you canget notified for the event. The Slider won't handle MouseDown events unless they come from the slider's... slider.
我想提一下 Slider 并没有完全吞下整个 MouseDown 事件。通过点击一个刻度,你可以得到通知的事件。Slider 不会处理 MouseDown 事件,除非它们来自滑块的...滑块。
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.
打勾的版本,请确保之前处理过该事件。如果你不这样做,你最终会得到一个边缘情况,你认为滑块被点击了,但它真的是一个勾号。注册预览事件甚至更糟 - 您可以在任何地方接收事件,即使是在滴答声之间的空白处。
回答by Derrick
Try using LostMouseCapture and GotMouseCapture.
尝试使用 LostMouseCapture 和 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.
GotMouseCapture 在用户开始拖动滑块时触发,而 LostMouseCapture 在用户释放滑块时触发。