首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >逐渐减小RepeatButton上的间隔

逐渐减小RepeatButton上的间隔
EN

Stack Overflow用户
提问于 2012-12-11 05:25:43
回答 2查看 808关注 0票数 1

我在我的项目中有一个可以左右滚动图形的RepeatButton。我希望当用户用鼠标按住按钮时,滚动速度会慢慢加快。据我所知,RepeatButton没有内置这个特性。有没有一种简单的方法,可以在用户按住按钮的同时慢慢减小按钮的间隔属性,然后在用户松开按钮时重新设置它?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-11 06:00:52

我认为这是不可能的。

Reason being是在幕后构建RepeatButton的方式。基本上,当您按空格键或按鼠标左键时,它会根据Interval属性的当前设置启动计时器。松开按键或鼠标按键时,计时器将停止。Interval属性只是被定义为一个普通的DependencyProperty,所以它不会做任何事情来更新计时器的时间间隔。

你也许可以通过反射来调整时间间隔,或者停止/重新启动计时器……但我不确定这是否真的值得。

票数 1
EN

Stack Overflow用户

发布于 2015-05-13 23:39:07

虽然有点麻烦,但通过扩展RepeatButton实际上很容易实现这种效果,只要您希望的间隔始终可以表示为某个恒定基本间隔的整数倍即可。

这里有一个快速示例,说明如何做到这一点:

代码语言:javascript
复制
public class AcceleratingRepeatButton : System.Windows.Controls.Primitives.RepeatButton
{
    private int _intervalCount = -1;
    protected override void OnClick()
    {
        //Always OnClick for the initial press
        if (_intervalCount < 0) {
            _intervalCount = -1;
            base.OnClick();
        } else if (ShouldTriggerRepeat(_intervalCount)) {
            base.OnClick();
        }
        _intervalCount += 1;
    }

    protected override void OnIsPressedChanged(DependencyPropertyChangedEventArgs e)
    {
        _intervalCount = -1;    //When the button is released, reset the counter
        base.OnIsPressedChanged(e);
    }
    
    private bool ShouldTriggerRepeat(int count)
    {
        const int intervalsAtQuarterRate = 16;   //For the first 16 intervals, trigger repeats at 1/4 the normal rate
        const int intervalsAtHalfRate = 16 + intervalsAtQuarterRate;    //the next 16 intervals at 1/2 the rate
         
        if (count < intervalsAtQuarterRate) {
            return (count & 7) == 0;    //Returns true every 4 intervals
        } else if (count < intervalsAtHalfRate) {
            return (count & 3) == 0;    //Returns true every 2 intervals
        } else {
            return true;
        }
    }
}

可以使用依赖属性来允许从xaml进行配置(为了简洁而排除),并且可以在ShouldTriggerRepeat中使用更复杂的逻辑来实现更微调的控制。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13809526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档