WPF popup showing double-click events

There are controls in my main application window, each of which opens a pop-up window that provides the user with more controls.

Other controls in the main application window have mousedoubleclick event handlers. My problem is that when the user double-clicks in the popup window, the controls behind the popup window receive mousedoubleclick events.

I tried to add the mousedoubleclick event handler to the popup parent and handle the event, but it still goes to the main application window.

private void ParentControl_MouseDoubleClick(object sender, MouseButtonEventArgs e) { e.Handled = true; } 

I also tried calling Popup.CaptureMouse () in the MouseEnter event handler in a popup, but the method always fails (returns false).

  void popup_MouseEnter(object sender, MouseEventArgs e) { e.Handled = true; Popup popup = sender as Popup; bool success = popup.CaptureMouse(); } 

Are there other ways to prevent mouse events from triggering in the main application window when the popup is open?

+4
source share
1 answer

Easily! Instead of using the MouseDoubleClick control

 private void myControl_MouseDoubleClick(System.Object sender, System.Windows.Input.MouseButtonEventArgs e) { MessageBox.Show("MouseDoubleClick on control"); } 

use the PreviewMouseDoubleClick event.

 private void myControl_PreviewMouseDoubleClick(System.Object sender, System.Windows.Input.MouseButtonEventArgs e) { MessageBox.Show("PreviewMouseDoubleClick on control"); } 

Now double-clicking on your control will also not trigger the DoubleClick parent event.

-one
source

All Articles