WPF WebBrowser control doesn't enter design mode when the document property is changed WPF WebBrowser control doesn't enter design mode when the document property is changed wpf wpf

WPF WebBrowser control doesn't enter design mode when the document property is changed


I'm not quite sure if we had exactly the same problem, but I suppose my solution should work for you as well.

The basic issue seems to be that x64 reset the designMode attribute, as noted in this article. In my case, I set it to "On" after instantiating the webbrowser, but in the DocumentCompleted event, it was "Inherit" again. Setting it back to "On" in DocumentCompleted makes it editable, but clears the document. Setting the DocumentText again restarts the whole doom loop.

So one solution I found was to refrain from setting the DocumentText, instead I created an empty document, then set the body's (which at this point is no longer null) InnerHtml property:

doc.designMode = "On"; // enable editing// designMode change resets the document, create it anewwebBrowser1.Document.Write("<html><body></body></html>")webBrowser1.Document.Body.InnerHtml = "myDocumentText"

Obviously, this works only if you have the text ready, and not if you're navigating to an URL. However, there is another solution which worked for me, which seems easier and safer. I found it in this answer by LaughingJohn. I guess the first line depends on your application, you had the IHTMLDocument directly in webBrowser1.Document.

doc = webBrowser1.Document.DomDocument as IHTMLDocument2;if (doc != null && doc.body != null)    ((HtmlBody)doc.body).contentEditable = "true";


It sounds to me like the WebBrowser gets the focus when you click on it and somehow holds on to it. Try this: click on the WebBrowser, then press the Tab key on the keyboard (which should move the focus off the WebBrowser) and then see if you can click on your buttons.

If you can, then try attaching a handler to the Button.MouseEnter event and call ((Button)sender).Foucs() in it to focus the button programmatically.