拖动时如何停止滑块的更新值? [英] How to stop update value of slider while dragging it?
问题描述
我有一个滑块,它的值绑定到某个属性,并且该属性一直在更新它.在拖动 [thumb on] 滑块时,我想阻止滑块的此更新值绑定,直到用户完成拖动.
I've slider that its value is bind to some property, and the property updates it all the time. While dragging the [thumb on] slider I want to stop this update value of slider from binding, until user finish it's dragging.
Slider 上是否有任何属性可以做到这一点,或者我需要为此编写代码?
Is there any property on Slider to do that, or I need to write code for that?
提前致谢!
推荐答案
Slider 的模板包含一个 Thumb,它会在鼠标移动时引发 ThumbDragDelta 事件.Slider 总是会在收到 ThumbDragDelta 事件时立即更新绑定值.
The template for Slider includes a Thumb, which raises the ThumbDragDelta event as the mouse is moved. Slider will always update the bound value immediately when it receives a ThumbDragDelta event.
诀窍是停止此事件.最简单的方法是继承 Slider:
The trick is to stop this event. The easiest way is to subclass Slider:
public class SliderIgnoreDelta : Slider
{
protected override void OnThumbDragDelta(DragDeltaEventArgs e)
{
// Do nothing
}
}
在拇指拖动完成之前,此滑块不会更新值.
This slider will not update the value until the thumb drag completes.
另一种解决方案是拦截 Thumb 上的 ThumbDragDelta 事件.如果您碰巧重新模板化 Slider,这可能是一个更好的解决方案.例如,如果您已经编写了一个 EventBlocker 类,在给定的 RoutedEvent 上将 Handled 设置为 true,您可以将其放入您的模板中:
Another solution is to intercept the ThumbDragDelta event on the Thumb. If you happen to be re-templating the Slider anyway, this might be a better solution. For example if you already have an EventBlocker class coded up that sets Handled true on the given RoutedEvent, you could put this in your template:
<Track.Thumb>
<Thumb my:EventBlocker.EventToBlock="{x:Static Thumb.DragDeltaEvent}" />
</Track.Thumb>
但在大多数情况下,您可能希望使用我的第一个解决方案.
But for most cases you'll probably want to go with my first solution.
这篇关于拖动时如何停止滑块的更新值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!