我怎样才能赶上WPF FrameworkElement的单单击和双击事件? [英] How can I catch both single-click and double-click events on WPF FrameworkElement?

查看:272
本文介绍了我怎样才能赶上WPF FrameworkElement的单单击和双击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能赶上一个单点击在这样一个TextBlock:

I can catch a single-click on a TextBlock like this:

private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("you single-clicked");
}



我能赶上一个双击上TextBlock的是这样的:

I can catch a double-click on a TextBlock like this:

private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        if (e.ClickCount == 2)
        {
            MessageBox.Show("you double-clicked");
        }
    }
}



但是如何我赶上他们都在一个单一的TextBlock和两个?

推荐答案

您需要后触发事件点击序列已经结束...当是什么?我建议使用一个计时器。 MouseDown事件将重新设置和提高点击次数。当计时器间隔结束它会调用评估点击计数

You need to fire the event after the click sequence is over... when is that? I suggest using a timer. The MouseDown event would reset it and increase the click count. When timer interval elapses it makes the call to evaluate the click count.

    private System.Timers.Timer ClickTimer;
    private int ClickCounter;

    public MyView()
    {
        ClickTimer = new Timer(300);
        ClickTimer.Elapsed += new ElapsedEventHandler(EvaluateClicks);
        InitializeComponent();
    }

    private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
    {
        ClickTimer.Stop();
        ClickCounter++;
        ClickTimer.Start();
    }

    private void EvaluateClicks(object source, ElapsedEventArgs e)
    {
        ClickTimer.Stop();
        // Evaluate ClickCounter here
        ClickCounter = 0;
    }



干杯!

Cheers!

这篇关于我怎样才能赶上WPF FrameworkElement的单单击和双击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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