WPF:按钮单击+双击问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/971459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF: Button single click + double click issue
提问by Hunsoul
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.
我必须以不同的反应处理 WPF 应用程序中按钮的单击和双击。不幸的是,在双击时,WPF 会触发两个单击事件和一个双击事件,因此很难处理这种情况。
It tried to solve it using a timer but without success...I hope you can help me.
它试图使用计时器解决它但没有成功......我希望你能帮助我。
Lets see the code:
让我们看看代码:
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
}
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
问题是在双击“双击操作”之后调用了“单击操作”。奇怪的是,我doubleClickTimer
在双击时将 设置为空,但这delayedBtnClick
是真的:O
I've already tried to use longer time, a bool flag and lock...
我已经尝试使用更长的时间,布尔标志和锁定...
Do you have any ideas?
你有什么想法?
Best!
最好的事物!
回答by rmoore
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
.
如果您在处理事件后将RoutedEvent
's设置e.Handled
为true
,MouseDoubleClick
则它不会Click
在MouseDoubleClick
.
There's a recent postwhich touches on having different behaviors for SingleClick
and DoubleClick
which may be useful.
最近有一篇文章涉及不同的行为SingleClick
并且DoubleClick
可能有用。
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.
但是,如果您确定想要单独的行为并且想要/需要阻止第一个Click
和第二个Click
,您可以DispatcherTimer
像以前一样使用。
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");
}
回答by Josh G
You could try this:
你可以试试这个:
Button.MouseLeftButtonDown += Button_MouseLeftButtonDown;
private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
if (e.ClickCount > 1)
{
// Do double-click code
}
else
{
// Do single-click code
}
}
If neccessary, you could require mouse click and wait until mouse up to perform the action.
如有必要,您可能需要单击鼠标并等待鼠标向上执行操作。