如何在不将 WPF 更改为三次单击的情况下重复双击?

发布于 2024-10-21 03:56:32 字数 1078 浏览 3 评论 0原文

我的 WPF 应用程序中有一个列表框,当用户双击某个项目时,我会更改列表的内容(类似于目录导航)。在某些情况下,双击然后立即再次双击是有意义的,因为您知道同一位置会出现什么。您可能希望只需快速连续单击四次(这应该意味着“双击两次”)即可做到这一点。

但这不起作用。如果您单击的速度足够快,那么直到第五次或第六次单击之后,WPF 才会发送另一个双击事件!

看起来 WPF 内置了对三次单击的支持,如果单击速度足够快,有时甚至更多。我运行了一个简单的示例来确认这一点:

<ListBox PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown"/>

private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Trace.WriteLine("ClickCount = " + e.ClickCount);
}

当我运行它并单击“连射”时,我得到如下输出:

ClickCount = 1
ClickCount = 2
ClickCount = 3
ClickCount = 1
ClickCount = 2
ClickCount = 3
ClickCount = 4
ClickCount = 1
ClickCount = 2
ClickCount = 3

有时它计数到 3,然后重置;有时它计数到 3,然后重置;有时会数到4才重置。我无法让它达到 5。

但后来我进入“控制面板”>“鼠标,并将我的双击速度更改为最慢的设置。然后,在重置之前,我可以可靠地将其数到 10,如果我真的预订了,则可以数到 11 或 12。因此,显然,这一切都与您在系统配置的“双击”间隔之前单击的速度有关。如果您在该间隔结束之前单击多次,您将获得“三次单击”或“四次单击”或更多次。

但我不想要三次点击或四次点击——尤其是在某些双击配置无法实现的情况下。我希望每次其他单击都是双击事件(只要它们快速连续,鼠标移动没有超过阈值量等)

有什么方法可以让 WPF 给我正常的当我单击连射时双击事件(每隔一次单击一个事件)?

I've got a listbox in my WPF app, and when the user double-clicks on an item, I change the contents of the list (something like directory navigation). In some cases, it makes sense to double-click, and then immediately double-click again, because you know what's going to appear in that same spot. You would expect that you could do that by just clicking four times in rapid succession (which ought to mean "two double-clicks").

But that doesn't work. If you click rapid-fire enough, it's not until the fifth or sixth click before WPF will send another double-click event!

It looks like WPF has built-in support for triple-clicks, and sometimes even more, if you click fast enough. I ran a quick example to confirm this:

<ListBox PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown"/>

private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Trace.WriteLine("ClickCount = " + e.ClickCount);
}

When I ran it, and clicked rapid-fire, I got output like this:

ClickCount = 1
ClickCount = 2
ClickCount = 3
ClickCount = 1
ClickCount = 2
ClickCount = 3
ClickCount = 4
ClickCount = 1
ClickCount = 2
ClickCount = 3

Sometimes it counted up to 3 and then reset; sometimes it counted up to 4 before it reset. I couldn't get it to go up to 5.

But then I went into Control Panel > Mouse, and changed my double-click speed to the slowest possible setting. Then I was reliably able to get it to count up to 10 before it reset, and up to 11 or 12 if I was really booking it. So apparently it's all about how fast you can click before your system-configured "double-click" interval. If you click more times before that interval has elapsed, you get a "triple-click" or a "quadruple-click" or more.

But I don't want triple-clicks or quadruple-clicks -- especially not if they're impossible to get with certain double-click configurations. I want every other click to be a double-click event (as long as they're in rapid succession, the mouse hasn't moved more than the threshold amount, etc.)

Is there any way I can get WPF to give me normal double-click events (an event for every other click) when I'm clicking rapid-fire?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向日葵 2024-10-28 03:56:32

使用几个实例变量:_LastMouseDownTime 和 _LastMouseDownWasDoubleClick:

    private int _LastMouseDownTime;
    private bool _LastMouseDownWasDoubleClick;

    private void ListBox_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
        if(e.Timestamp - _LastMouseDownTime <= System.Windows.Forms.SystemInformation.DoubleClickTime && !_LastMouseDownWasDoubleClick) {
            Debug.Print("DoubleClick - StupidWorthlessClickCount:{0}", e.ClickCount);
            _LastMouseDownWasDoubleClick = true;

            //Do some double clicking goodness
        } else {
            Debug.Print("FirstClick - StupidWorthlessClickCount:{0}", e.ClickCount);
            _LastMouseDownWasDoubleClick = false;

            //Do some single clicking goodness
        }
        _LastMouseDownTime = e.Timestamp;
        base.OnMouseDown(e);
    }

我知道这是超级 hacky 并且不是很灵活,但它应该可以满足您的需要。我试图让 WPF RichTextBox 响应第四次、第五次等点击,就像第一次、第二次等点击一样,以使选择工作更加自然,但这可能被证明是不可能的! :)

希望这有帮助。

Use a couple of Instance Variables: _LastMouseDownTime and _LastMouseDownWasDoubleClick:

    private int _LastMouseDownTime;
    private bool _LastMouseDownWasDoubleClick;

    private void ListBox_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
        if(e.Timestamp - _LastMouseDownTime <= System.Windows.Forms.SystemInformation.DoubleClickTime && !_LastMouseDownWasDoubleClick) {
            Debug.Print("DoubleClick - StupidWorthlessClickCount:{0}", e.ClickCount);
            _LastMouseDownWasDoubleClick = true;

            //Do some double clicking goodness
        } else {
            Debug.Print("FirstClick - StupidWorthlessClickCount:{0}", e.ClickCount);
            _LastMouseDownWasDoubleClick = false;

            //Do some single clicking goodness
        }
        _LastMouseDownTime = e.Timestamp;
        base.OnMouseDown(e);
    }

I know this is super hacky and not very flexible, but it should get you what you need. I am trying to get WPF RichTextBox to respond to those 4th, 5th, etc. clicks like they were the 1st, 2nd, etc. to make selection work more naturally, but this may prove to be impossible! :)

Hope this helps.

无戏配角 2024-10-28 03:56:32

ClickCount 显示用户在系统双击时间内点击的次数。因此,如果您只需要在双击时做出响应:

private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount % 2 == 1)
        return;
    Trace.WriteLine(e.ClickCount + " " + DateTime.Now);
}

输出:

//2 clicks:
2 10.03.2011 16:05:17
//4 clicks:
2 10.03.2011 16:05:20
4 10.03.2011 16:05:20
//3 clicks:
2 10.03.2011 16:05:26

ClickCount shows how many times user had clicked within the system double click time. So if you need to respond only on double clicks:

private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount % 2 == 1)
        return;
    Trace.WriteLine(e.ClickCount + " " + DateTime.Now);
}

Output:

//2 clicks:
2 10.03.2011 16:05:17
//4 clicks:
2 10.03.2011 16:05:20
4 10.03.2011 16:05:20
//3 clicks:
2 10.03.2011 16:05:26
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文