This is a mirror of official site: http://jasper-net.blogspot.com/

Binding Html to the Web Browser Control

| Tuesday, October 5, 2010
Currently the fastest way to display a string of html on the Windows Phone 7 is to use the Web Browser control, simply call the NavigateToString method passing the html you want to display.

When trying to use this in an MVVM pattern the method call becomes slightly problematic, we'd much rather have a bindable property. Thankfully we can use attached properties to achieve this.

public static class WebBrowserHelper
{
   public static readonly DependencyProperty HtmlProperty = DependencyProperty.RegisterAttached(
       "Html", typeof(string), typeof(WebBrowserHelper), new PropertyMetadata(OnHtmlChanged));

   public static string GetHtml(DependencyObject dependencyObject)
   {
       return (string)dependencyObject.GetValue(HtmlProperty);
   }

   public static void SetHtml(DependencyObject dependencyObject, string value)
   {
       dependencyObject.SetValue(HtmlProperty, value);
   }

   private static void OnHtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   {
       var browser = d as WebBrowser;

       if(browser == null)
           return;

Read more: compiled experience

Posted via email from .NET Info

0 comments: