题
我必须同时处理所述单个点击一个按钮的在具有不同反应WPF应用程序中的双击。 不幸的是,上双击,WPF触发两次单击事件和双击事件,所以很难处理这种情况。
有尝试过使用定时器,但没有成功。我希望你能帮助我解决。
让我们看看的代码:
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
}
在的问题是,“单点击动作”上双击的“双击动作”之后被调用。这是奇怪的是,我设置thedoubleClickTimer
为null上双击,但在delayedBtnClick
这是真的:0
我已经尝试过使用更长的时间,一个布尔标志和锁...
你有什么想法?
最佳!
解决方案
如果您设置RoutedEvent
的e.Handled
到true
处理MouseDoubleClick
事件再经过它不会调用Click
事件MouseDoubleClick
后的第二次。
有href="https://stackoverflow.com/questions/970898/double-click-vs-single-click-are-these-mutually-exclusive">最近帖子这倒是一个SingleClick和DoubleClick
这可能是有用的不同的行为。
不过,如果你确信你想单独行为,并希望/需要阻止第一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");
}
其他提示
您可以试试这个:
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
}
}
如果有必要,你需要点击鼠标,等到鼠标向上执行的操作。
不隶属于 StackOverflow