RightDoubleClick and context menu
Related Question (Details)
Tunneling events and ContextMenu
I have a WPF canvas to which I associated a ContextMenu..
This is cool. Now I have to implement some action on Right DoubleClick...
In fact, I never receive on Right mouse ClickCount == 2...
What to do?
I need to display ContextMenu on simple (right) click, and perform Action2 OnRightDoubleClick..protected override void OnPreviewMouseRightButtonUp(MouseButtonEventArgs e)
{
if (e.Click开发者_StackOverflow社区Count == 1)
{
#region SINGLE CLICK
stillSingleClick = true;
Thread thread = new Thread(
new System.Threading.ThreadStart(
delegate()
{
Thread.Sleep(System.Windows.Forms.SystemInformation.DoubleClickTime);
this.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Background,
new Action(
delegate()
{
if (stillSingleClick)
{
base.OnPreviewMouseRightButtonUp(e);
}
stillSingleClick = false;
}
));
}
));
thread.Start();
#endregion SINGLE CLICK
}
else if (e.ClickCount == 2)
{
stillSingleClick = false;
base.OnPreviewMouseRightButtonUp(e);
}
}
MouseButtonEventArgs.ClickCount
will always be 1 since you are handling an up event not a down event. Both the PreviewUp and Up will always be 1. The click behavior is usually defined as the down event of the respective button.
Do this in MouseDoubleClickEvent
if (e.ChangedButton == MouseButton.Right)
{
//do something with Mouse Right Double Click
}
Check out this example at MSDN:
private void OnMouseDownClickCount(object sender, MouseButtonEventArgs e)
{
//Handle only right clicks
if (e.RightButton != MouseButtonState.Pressed) return;
// Checks the number of clicks.
if (e.ClickCount == 1)
{
// Single Click occurred.
lblClickCount.Content = "Single Click";
}
if (e.ClickCount == 2)
{
// Double Click occurred.
lblClickCount.Content = "Double Click";
}
if (e.ClickCount >= 3)
{
// Triple Click occurred.
lblClickCount.Content = "Triple Click";
}
}
精彩评论