简体   繁体   中英

WPF | WindowFormHost | WebBrowser | Context Menu

I am trying to write an application where users can highlight text in a browser and mark the contents via the context menu. I have a WPF application and I am using the WindowsFormHost with a Windows Forms WebBrowser so I can have a custom context menu. My problem is that I cannot get the Click event to fire.

Am I doing something wrong?

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitalizeComponent();            
        System.Windows.Forms.WebBrowser Browser = new System.Windows.Forms.WebBrowser();              
        Browser.IsWebBrowserContextMenuEnabled = false;
        System.Windows.Forms.ContextMenu BrowserContextMenu = new System.Windows.Forms.ContextMenu();       
        System.Windows.Forms.MenuItem MenuItem = new System.Windows.Forms.MenuItem();
        MenuItem.Text = "Take Action";             
        MenuItem.Click += new System.EventHandler(this.MenuItem_Click);
        BrowserContextMenu.MenuItems.Add(MenuItem);
        Browser.ContextMenu = BrowserContextMenu;
        WindowsFormsHost.Child(Browser);               
    }

    //THIS DOESN'T FIRE!?!?!?
    private void MenuItem_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Click!");
    }
}

将所有内容移至Main_Loaded并成功

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM