从WPF到WinForms的气泡鼠标事件

从WPF到WinForms的气泡鼠标事件,wpf,mouse,event-bubbling,elementhost,Wpf,Mouse,Event Bubbling,Elementhost,我使用ElementHost将WPF控件托管在WinForms控件中。WinForms控件具有上下文菜单。我想在用户右键单击WPF控件时显示上下文菜单。如何做到这一点?似乎鼠标事件没有从WPF冒泡到WinForms。它不会自动冒泡,因为您可能首先在WPF控件中处理过它。但是,您可以自己轻松地添加此项 在WPF用户控件中,公开鼠标右键向上触发的事件: public event Action ShowContext; private void rectangle1_MouseRig

我使用ElementHost将WPF控件托管在WinForms控件中。WinForms控件具有上下文菜单。我想在用户右键单击WPF控件时显示上下文菜单。如何做到这一点?似乎鼠标事件没有从WPF冒泡到WinForms。

它不会自动冒泡,因为您可能首先在WPF控件中处理过它。但是,您可以自己轻松地添加此项

在WPF用户控件中,公开鼠标右键向上触发的事件:

    public event Action ShowContext;

    private void rectangle1_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (ShowContext != null)
        {
            ShowContext();
        }
    }
然后,在带有元素主机的winforms控件中,您可以这样使用它:

    public UserControl1 WpfControl { get; set; }

    public Form1()
    {
        InitializeComponent();

        WpfControl = new UserControl1();
        WpfControl.ShowContext += () => contextMenuStrip1.Show(Cursor.Position);
        elementHost1.Child = WpfControl;
     ....