WPF按钮需要两次点击来触发Click事件 [英] WPF button takes two clicks to fire Click event
问题描述
我有一个TabItem包含一个日历控件和一个按钮。问题是,当日历的选定日期与之前选择的日期相同时,该按钮需要两次点击来触发其Click事件。
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.
我实现了selectedDatesChanged事件的日历来解决这个问题,当前选择的日期不同于以前的选择。代码如下:
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();
}
我要找的是一种方法,当日历的selectedDate与以前选择的日期没有不同时,上述功能。
我尝试处理GotFocus和MouseUp事件,但它不能解决问题。
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?
感谢您,
Naveen
Thanks, Naveen
推荐答案
最好的答案我在网上找到。它仍然不完美,因为它不能帮助标记为IsDefault或IsCancel
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);
}
}
这篇关于WPF按钮需要两次点击来触发Click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!