Thomas Freudenberg

Confessions of a caffeine addict

Binding WebBrowser content in WPF

When you’re using a WebBrowser control in your WPF application, you may have noticed that you can’t bind the control’s content. WebBrowser has no property to set its content but a method named NavigateToString. So when you’re following a strict MVVM approach you’re lost because you don’t want any code-behind for your views.

But then there are attached properties. As their name implies they allow you to attach new properties to existing dependency objects. In your XAML code you apply such a attached property to your element and can access it as any other property of the object.

Ok, first here’s the code of an attached property to set a WebBrowser’s content:

public class WebBrowserHelper {
    public static readonly DependencyProperty BodyProperty =
        DependencyProperty.RegisterAttached("Body", typeof (string), typeof(WebBrowserHelper), new PropertyMetadata(OnBodyChanged));

    public static string GetBody(DependencyObject dependencyObject) {
        return (string) dependencyObject.GetValue(BodyProperty);
    }

    public static void SetBody(DependencyObject dependencyObject, string body) {
        dependencyObject.SetValue(BodyProperty, body);
    }

    private static void OnBodyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        var webBrowser = (WebBrowser) d;
        webBrowser.NavigateToString((string)e.NewValue);
    }
}

The static BodyProperty defines the type of the attached property: its name is Body, the type is string, and whenever it is changed the method OnBodyChanged should be called.

The accessors for a attached property must be conventionally named SetXxx and GetXxx. They are called whenever you set or get the property’s value.

Last but not least OnBodyChanged is called when the value of the property has changed. The first parameter is the object the property is attached to, so we can cast it to WebBrowser and call its NavigateToString method.

The actual usage of the new Body property is pretty simple:

<WebBrowser
    src:WebBrowserHelper.Body="{Binding MyHtml}"
    />

given that the ViewModel has a property named MyHtml providing the desired content for the control.

A complete sample application is available on GitHub.

Comments

Thomas Freudenberg said:

Since Windows 7 the icon of an application can get an overlay bitmap. You can use that to indicate some

# August 15, 2010 11:35 AM

yo said:

hi

can u post this in vb.net?

im having difficulty translating the first line. (DependencyProperty.RegisterAttached)

thanks!

# May 23, 2011 12:41 PM

Kari said:

Hi,

Its not working. I get the error at run time xaml markup threw an exception.

Please provide the appropriate solution.

Thanks

# July 22, 2011 11:08 AM

WPF, Webbrowser & Content DataBinding « Istace Emmanuel said:

Pingback from  WPF, Webbrowser &amp; Content DataBinding &laquo; Istace Emmanuel

# August 2, 2011 5:58 PM

The dood said:

Thanks mate

worked for me.  

# August 15, 2011 5:18 AM

Tanuj Kumar said:

Very nice article. I really enjoyed it reading. And it also cleared lot of my doubts about WPF WebBrowser control. Some other article too helped me lot in completing my task. These article are...

mindstick.com/.../3610ee37-4b56-41fd-99b1-c4805b016901

www.c-sharpcorner.com/.../web-browser-control-in-wpf

msdn.microsoft.com/.../system.windows.controls.webbrowser.aspx

# March 13, 2012 11:41 AM

Madhu Ranjan said:

Good article solved my problem of not having Source as a dependency property, used same to bind with Source

Thanks

# June 2, 2012 12:03 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)