WPF:按钮单击+双击问题 [英] WPF: Button single click + double click issue
问题描述
我必须以不同的反应处理 WPF 应用程序中按钮的单击和双击.不幸的是,在双击时,WPF 会触发两个单击事件和一个双击事件,因此很难处理这种情况.
I have to handle both the single click and the double click of a button in a WPF application with different reaction. Unfortunately, on a doubleclick, WPF fires two click event and a double click event, so it's hard to handle this situation.
它尝试使用计时器解决它但没有成功...希望你能帮助我.
It tried to solve it using a timer but without success...I hope you can help me.
让我们看看代码:
private void delayedBtnClick(object statInfo)
{
if (doubleClickTimer != null)
doubleClickTimer.Dispose();
doubleClickTimer = null;
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new VoidDelegate(delegate()
{
// ... DO THE SINGLE CLICK ACTION
}));
}
private void btn_Click(object sender, RoutedEventArgs e)
{
if (doubleClickTimer == null)
doubleClickTimer = new Timer(delayedBtnClick, null, System.Windows.Forms.SystemInformation.DoubleClickTime, Timeout.Infinite);
}
}
}
private void btnNext_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (doubleClickTimer != null)
doubleClickTimer.Change(Timeout.Infinite, Timeout.Infinite); // disable it - I've tried it with and without this line
doubleClickTimer.Dispose();
doubleClickTimer = null;
//.... DO THE DOUBLE CLICK ACTION
}
问题是在双击双击操作"之后调用了单击操作".奇怪的是我在双击时将 doubleClickTimer
设置为 null 但在 delayedBtnClick
中它是真的:O
The problem is that the 'SINGLE CLICK ACTION' called after the 'DOUBLE CLICK ACTION' on doubleclick. It's strange that I set thedoubleClickTimer
to null on double click but in the delayedBtnClick
it's true :O
我已经尝试使用更长的时间,一个布尔标志和锁...
I've already tried to use longer time, a bool flag and lock...
你有什么想法吗?
最好!
推荐答案
如果将 RoutedEvent
的 e.Handled
设置为 true
处理完 MouseDoubleClick
事件后,它不会在 MouseDoubleClick
之后第二次调用 Click
事件.
If you set the RoutedEvent
's e.Handled
to true
after handling the MouseDoubleClick
event then it will not call the Click
Event the second time after the MouseDoubleClick
.
有一篇最近的帖子SingleClick
和 DoubleClick
有不同的行为,这可能很有用.
There's a recent post which touches on having different behaviors for SingleClick
and DoubleClick
which may be useful.
但是,如果您确定想要单独的行为并且想要/需要阻止第一个 Click
以及第二个 Click
,您可以使用 DispatcherTimer
就像你一样.
However, if you are sure you want separate behaviors and want/need to block the first Click
as well as the second Click
, you can use the DispatcherTimer
like you were.
private static DispatcherTimer myClickWaitTimer =
new DispatcherTimer(
new TimeSpan(0, 0, 0, 1),
DispatcherPriority.Background,
mouseWaitTimer_Tick,
Dispatcher.CurrentDispatcher);
private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// Stop the timer from ticking.
myClickWaitTimer.Stop();
Trace.WriteLine("Double Click");
e.Handled = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
myClickWaitTimer.Start();
}
private static void mouseWaitTimer_Tick(object sender, EventArgs e)
{
myClickWaitTimer.Stop();
// Handle Single Click Actions
Trace.WriteLine("Single Click");
}
这篇关于WPF:按钮单击+双击问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!