Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Tip/Trick

WPF WebBrowser Control Enhancements

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
28 Nov 2016CPOL2 min read 18.1K   833   7   2
The WPF WebBrower control leaves a lot to be desired.

Introduction

The WPF WebBrowser control is extremely sad in the capabilities it provides, and even in supporting the MVVM pattern. I created a control to enhance the capabilities of the WebBrower to support easy interface using ICommand for forward and back buttons, and the MVVM design pattern. Since Microsoft made things even sillier because the WebBrowser is Sealed, I had to inherit from the ContentControl and this ContentControl only contains a WebBrowser control.

The following are the capabilities this control has:

  • NavigateString (string) DependencyProperty which will execute the WebBrowser Navigate method.
  • HeaderString (string) DependencyProperty which will set the cookies for the brower. The string contains the cookie items separated by a ";" with each cooking being the key, an equal sign ("=") and then the value.
  • An IsSuccessful DependencyProperty that is set to null when navigation is started, and then changes to true if it appears that the navigation was successful, and false is not. The way it is determined that the navigation was not successful is if the Document Url property starts with "res://ieframe.dll".
  • A Body DependencyProperty that will allow the content of the WebBrowser to be defined by a string containing HTML.
  • A BrowseForward and BrowseBack ICommand that allows a Button to be bound to the WebBrowserControl for navigation forward and back.
  • The silencing of error messages by the browser that are probably due to javascript errors (I was having a lot of trouble with this on some sites I browsed to).
  • A way to discover and update the Internet Explorer browser version used by the control.
  • Ability to disable/enable WebBrowser ContextMenu (disabled by default)

Using the code

This is an example of using the control using the MVVM design pattern

XML
<local:WebBrowserControl x:Name="Browser"
                         HorizontalAlignment="Stretch"
                         VerticalAlignment="Stretch"
                         HeaderString="{Binding WebCookies}"
                         IsSuccessful="{Binding IsSuccessful, Mode=OneWayToSource}"
                         NavigateString="{Binding WebUri}" />

Note

The sample uses the URI "http://request.urih.com/" because this will show the cookies.

Image 1

The sample above has red boxes to show where the token information can be found.

Conclusion

This control could be significantly enhanced, but includes a lot of snippets that should be quite useful to anyone trying to implement a WebBrowser in WPF, and also is a good template for implementing enhancements. If anyone has any thoughts on how this can be improved, I am definitely interested.

History

  • 2016-11-28: Initial Version
  • 2017-01-20: Added abiltiy to remove WebBrowser ContextMenu (thanks to code provided by Simon Mourier

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at qck1@hotmail.com.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Farhad Reza2-Dec-16 6:13
Farhad Reza2-Dec-16 6:13 
AnswerRe: My vote of 5 Pin
Clifford Nelson2-Dec-16 9:25
Clifford Nelson2-Dec-16 9:25 
Thanks. Should be of help. If you have any ideas on what you would like flushed out, I am happy to look into it. As I stated in the article I would consider this somewhat crude. I am using it, but still waiting for other developers to add what is needed, and then to have a truly working application.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.