Bubble mouse event from WPF to WinForms

I have a WPF control hosted inside a WinForms control using ElementHost. The WinForms control has a context menu. I want to show a context menu when a user right-clicks on a WPF control. How can this be done? The mouse event does not seem to bubble from WPF to WinForms.

+4
source share
1 answer

it will not be automatically activated since you might have processed it in the WPF control first. However, you can easily add this yourself.

In the WPF user control, output the event that you fire with the right mouse button:

public event Action ShowContext; private void rectangle1_MouseRightButtonUp(object sender, MouseButtonEventArgs e) { if (ShowContext != null) { ShowContext(); } } 

Then in your winforms control using the element host you can use it like this:

  public UserControl1 WpfControl { get; set; } public Form1() { InitializeComponent(); WpfControl = new UserControl1(); WpfControl.ShowContext += () => contextMenuStrip1.Show(Cursor.Position); elementHost1.Child = WpfControl; .... 
+4
source

All Articles