WPF 按钮需要单击两次才能触发 Click 事件

发布于 2024-10-30 01:35:03 字数 456 浏览 1 评论 0原文

我有一个 TabItem,其中包含一个日历控件和一个按钮。问题是,当日历的选择日期与先前选择的日期相同时,该按钮需要单击两次才能触发其 Click 事件。

我实现了日历的 selectedDatesChanged 事件来解决当当前选择的日期与之前选择的日期不同时的这个问题。代码如下:

selectedDatesChanged(object sender, SelectionChangedEventArgs e)
{
    this.CaptureMouse();
    this.ReleaseMouseCapture();
}

我正在寻找的是一种当日历的 selectedDate 与先前选择的日期没有不同时具有与上述函数所示相同效果的方法。 我尝试处理 GotFocus 和 MouseUp 事件,但并不能解决问题。

有人对我如何解决这个问题有任何想法吗?

谢谢, 纳文

I have a TabItem which contains a calendar control and a button. The issue is that when the calendar's selected date is the same as the previously selected date, the button takes two clicks to fire its Click event.

I have implemented the selectedDatesChanged event of the calendar to solve this problem when the current selected date is different from the previous selection. The code is as below:

selectedDatesChanged(object sender, SelectionChangedEventArgs e)
{
    this.CaptureMouse();
    this.ReleaseMouseCapture();
}

What I'm looking for is a way to have the same effect shown in the above function when the selectedDate of the calendar does not differ from the previously selected date.
I tried handling the GotFocus and MouseUp events, but it doesn't solve the problem.

Does anyone have any ideas on how I could solve this issue?

Thanks,
Naveen

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

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

发布评论

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

评论(2

灵芸 2024-11-06 01:35:03

这是我在网上找到的最好答案。它仍然不完美,因为它对标记为 IsDefault 或 IsCancel 的按钮没有帮助

protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
{
  base.OnPreviewMouseUp(e);
  if (Mouse.Captured is Calendar || Mouse.Captured is System.Windows.Controls.Primitives.CalendarItem)
  {
    Mouse.Capture(null);
  }
}

This was the best answer I found on the web. It's still not perfect because it doesn't help with buttons that are marked as IsDefault or IsCancel

protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
{
  base.OnPreviewMouseUp(e);
  if (Mouse.Captured is Calendar || Mouse.Captured is System.Windows.Controls.Primitives.CalendarItem)
  {
    Mouse.Capture(null);
  }
}
倾城月光淡如水﹏ 2024-11-06 01:35:03

你可以简单地写:

Mouse.Capture(null);

这将解决鼠标保持焦点的问题

You could simply write:

Mouse.Capture(null);

This will solve the issue of mouse holding focus

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