21

我可以像这样单击TextBlock:

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

我可以像这样双击TextBlock:

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

但是我如何在一个 TextBlock 上同时捕捉它们并区分两者?

4

9 回答 9

13

您需要在点击序列结束后触发事件......那是什么时候?我建议使用计时器。MouseDown 事件将重置它并增加点击次数。当计时器间隔过去时,它会调用以评估点击计数。

    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;
    }

干杯!

于 2010-01-18T16:29:21.863 回答
4

如果您需要检测差异,我建议您使用Label可以为您工作的控件:

label.MouseDown += delegate(object sender, MouseEventArgs e)
{
    if (e.ClickCount == 1)
    {
        // single click
    }
};

label.MouseDoubleClick += delegate
{
    // double click
};

编辑:我的建议来自 MSDN 上的文档:

Control 类定义了 PreviewMouseDoubleClick 和 MouseDoubleClick 事件,但没有定义相应的单击事件。要查看用户是否单击过一次控件,请处理 MouseDown 事件(或其对应事件之一)并检查 ClickCount 属性值是否为 1。

但是,即使用户单击,这样做也会给您一个单击通知。

于 2010-01-18T13:41:12.387 回答
2

您必须使用计时器来区分两者。在 GUI 中为您的表单添加一个计时器(最简单的方式 - 它会自动处理处置等......)。在我的示例中,计时器被调用clickTimer

private bool mSingleClick;

private void TextBlock_MouseUp(object sender, MouseButtonEventArgs e)
{

    if (e.Button == MouseButtons.Left)
    {
        if (e.ClickCount < 2)
        {
            mSingleClick = true;
            clickTimer.Interval = System.Windows.Forms.SystemInformation.DoubleClickTime;
            clickTimer.Start();
        }
        else if (e.ClickCount == 2)
        {
            clickTimer.Stop();
            mSingleClick = false;
            MessageBox.Show("you double-clicked");
        }
    }
}

private void clickTimer_Tick(object sender, EventArgs e)
{
    if (mSingleClick)
    {
        clickTimer.Stop();
        mSingleClick = false;
        MessageBox.Show("you single-clicked");
    }
}
于 2011-06-22T09:32:42.187 回答
1

您可以简单地使用MouseDown事件并计算点击次数,如下所示:

if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
{
    // your code here
}
于 2014-04-07T13:53:52.467 回答
1

我这样做了,而且效果很好

If e.Clicks = 2 Then
            doubleClickTimer.Stop()
        ElseIf e.Clicks = 1 Then
            doubleClickTimer.Enabled = True
            doubleClickTimer.Interval = 1000
            doubleClickTimer.Start()


        End If


 Private Sub doubleClickTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles doubleClickTimer.Tick

        OpenWebPage("abc")
        doubleClickTimer.Stop()
    End Sub
于 2012-08-22T16:45:40.360 回答
1

我的建议,UserControl通过简单地使用 a 来实现Task

private int _clickCount = 0;
protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
{
    _clickCount = e.ClickCount;
}

protected override async void OnPreviewMouseUp(MouseButtonEventArgs e)
{
    if (_clickCount > 1)
    {
        //apparently a second mouse down event has fired => this must be the second mouse up event
        //no need to start another task
        //the first mouse up event will be handled after the task below
        return; 
    }

    await Task.Delay(500);

    if (_clickCount == 1)
    {
        //single click
    }
    else
    {
        //double (or more) click
    }
}

当然,所有这些解决方案的缺点是在实际响应用户操作之前会有延迟。

于 2020-03-03T00:05:28.467 回答
0

您可以在 MouseUp 而不是 MouseDown 上执行此操作。这样您就可以向ClickCount酒店询问总点击次数,然后决定从该点开始做什么。

于 2011-06-25T12:26:13.993 回答
0

这是我的工作解决方案:)

    #region message label click --------------------------------------------------------------------------
private Timer messageLabelClickTimer = null;

private void messageLabel_MouseUp(object sender, MouseButtonEventArgs e)
{
  Debug.Print(e.ChangedButton.ToString() + " / Left:" + e.LeftButton.ToString() + "  Right:" + e.RightButton.ToString() + "  click: " + e.ClickCount.ToString());
  // in MouseUp (e.ClickCount == 2) don't work!!  Always 1 comes.
  // in MouseDown is set e.ClickCount succesfully  (but I don't know should I fire one clicked event or wait second click)

  if (e.ChangedButton == MouseButton.Left)
  {
    if (messageLabelClickTimer == null)
    {
      messageLabelClickTimer = new Timer();
      messageLabelClickTimer.Interval = 300;
      messageLabelClickTimer.Elapsed += new ElapsedEventHandler(messageLabelClickTimer_Tick);
    }

    if (! messageLabelClickTimer.Enabled)                                                 
    { // Equal: (e.ClickCount == 1)
      messageLabelClickTimer.Start();
    }
    else  
    { // Equal: (e.ClickCount == 2)
      messageLabelClickTimer.Stop();

      var player = new SoundPlayer(ExtraResource.bip_3short);               // Double clicked signal
      player.Play();
    }
  }
}

private void messageLabelClickTimer_Tick(object sender, EventArgs e)
{ // single-clicked
  messageLabelClickTimer.Stop();

  var player = new SoundPlayer(ExtraResource.bip_1short);                  // Single clicked signal
  player.Play();
}    
#endregion
于 2015-02-25T13:22:59.397 回答
0

DataGrid我的问题是在in 中单击/双击行WPF。出于某种原因,ButtonDown事件没有触发,只有OnMouseLeftButtonUp事件触发了。无论如何,我想以不同于双击的方式处理单击。看起来我需要一点时间(我确信解决方案并不完美,但它似乎有效)来提炼问题,直到我把它归结为下面。我创建了一个Task调用 anAction并且该Action目标可以通过第二次单击来更新。希望这对某人有帮助!

private Action _clickAction;
private int _clickCount;

private void Grid_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    Debug.WriteLine("Button Click Occurred");

    _clickCount++;

    if (_clickCount == 1)
    {
        _clickAction = SingleClick;
    }

    if (_clickCount > 1)
    {
        _clickAction = DoubleClick;
    }

    if (_clickCount == 1)
    {
        Task.Delay(200)
            .ContinueWith(t => _clickAction(), TaskScheduler.FromCurrentSynchronizationContext())
            .ContinueWith(t => { _clickCount = 0; });
    }
}

private void DoubleGridClick()
{
    Debug.WriteLine("Double Click");
}

private void SingleGridClick()
{
    Debug.WriteLine("Single Click");
}
于 2020-01-24T13:57:24.717 回答