WPF:带有在用户拖动后触发的事件的滑块 [英] WPF: Slider with an event that triggers after a user drags

查看:50
本文介绍了WPF:带有在用户拖动后触发的事件的滑块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 WPF 中制作 MP3 播放器,我想制作一个滑块,允许用户通过向左或向右滑动滑块来寻找 MP3 中的特定位置.

I am currently making an MP3 player in WPF, and I want to make a slider that will allow the user to seek to a particular position in an MP3 by sliding the slider to the left or right.

我曾尝试使用 ValueChanged 事件,但每次更改其值时都会触发,因此如果您将其拖过,该事件将触发多次,我希望该事件仅当用户完成拖动滑块时触发,然后获取新值.

I have tried using the ValueChanged event but that triggers every time it's value is changed, so if you drag it across, the event will fire multiple times, I want the event to only fire when the user has finished dragging the slider and Then get the new value.

我怎样才能做到这一点?

How can I achieve this?

[更新]

我找到了 MSDN上的这篇文章基本上讨论了同一件事,他们提出了两个解决方案";子类化 Slider 或在 ValueChanged 事件中调用 DispatcherTimer 以在时间跨度后调用操作.

I have found this post on MSDN which basically discusses the same thing, and they came up with two "solutions"; either subclassing the Slider or invoking a DispatcherTimer in the ValueChanged event that invokes the action after a timespan.

你能想出比上面提到的两个更好的东西吗?

Can you come up with anything better then the two mentioned above?

推荐答案

您可以为此使用拇指的 'DragCompleted' 事件.不幸的是,这仅在拖动时触发,因此您需要单独处理其他点击和按键.如果您只希望它可拖动,则可以通过将 LargeChange 设置为 0 并将 Focusable 设置为 false 来禁用这些移动滑块的方法.

You can use the thumb's 'DragCompleted' event for this. Unfortunately, this is only fired when dragging, so you'll need to handle other clicks and key presses separately. If you only want it to be draggable, you could disable these means of moving the slider by setting LargeChange to 0 and Focusable to false.

示例:

<Slider Thumb.DragCompleted="MySlider_DragCompleted" />

这篇关于WPF:带有在用户拖动后触发的事件的滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆