像VLC这样的音量滑块 [英] Volume Slider like VLC

查看:128
本文介绍了像VLC这样的音量滑块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索外观和行为类似于VLC滑块的Volume Slider。

I am searching for Volume Slider that looks and behave just like VLC's slider.

我发现了以下有关如何设置滑块样式的文章:音量滑块CustomControl

,但我也想要相同的行为...

I found the following post about how to style the slider: Volume Slider CustomControl
but I also want the same behavior...

什么是行为之间的区别:
单击滑块[在WPF处]并将鼠标移到滑块区域上(同时按住鼠标按钮不放),它也应将滑块也移到鼠标在鼠标上的位置。

What is the difference between the behaviors: When you click on the slider [at the WPF] and move the mouse on the slider area (while mouse button still being hold) it should move the slider also to the position of the mouse on the slider.

我找不到方法..也许我应该使用与Slider不同的东西?

I couldn't find how to do it.. maybe I should use something different than Slider?

感谢您的帮助!

推荐答案

滑块上有一个名为 IsMoveToPointEnabled ,可将滑块设置为正确的值,但只有在单击时,滑块才会随着拖动而更新。

There is a property on the slider called IsMoveToPointEnabled that sets the slider to the correct value but only when you click it doesn't update as you drag.

在拖动时进行更新您必须在移动鼠标时自行更新值,方法 Track.ValueFromPoint 为您提供正确的值,该轨道是滑块模板的一部分。

To update as you drag you have to update the value yourself when the mouse is moved, the method Track.ValueFromPoint gives you the correct value, the track is part of the sliders template.

示例

public class DraggableSlider : Slider
{
    public DraggableSlider()
    {
        this.IsMoveToPointEnabled = true;
    }

    private Track track;
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        track = Template.FindName("PART_Track", this) as Track;
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        if(e.LeftButton == MouseButtonState.Pressed && track != null)
        {
            Value = track.ValueFromPoint(e.GetPosition(track));
        }
    }


    protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
    {
        base.OnPreviewMouseDown(e);
        ((UIElement)e.OriginalSource).CaptureMouse();
    }

    protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
    {
        base.OnPreviewMouseUp(e);
        ((UIElement)e.OriginalSource).ReleaseMouseCapture();
    }
}

OnPreviewMouseUp / Down会覆盖鼠标,我尝试过VLC并不能捕获鼠标,因此您可以根据需要删除它们。即使鼠标离开控件(类似于滚动条的工作方式),捕获鼠标也会允许该值更改。

The OnPreviewMouseUp/Down overrides capture the mouse, I tried VLC and that doesn't capture the mouse so you could remove them if you wanted. Capturing the mouse allows the value to change even if the mouse leaves the control similar to how scrollbars work.

这篇关于像VLC这样的音量滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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