Display html from string in WPF WebBrowser control

The data context object contains a string property that returns the html that should be displayed in the WebBrowser control; I cannot find any WebBrowser properties for binding. Any ideas?

Thank!

+49
wpf webbrowser-control
Apr 6 2018-10-06T00:
source share
1 answer

WebBrowser has a NavigateToString method that you can use to navigate to HTML content. If you want to be able to bind to it, you can create an attached property that can simply call the method when the value changes:

 public static class BrowserBehavior { public static readonly DependencyProperty HtmlProperty = DependencyProperty.RegisterAttached( "Html", typeof(string), typeof(BrowserBehavior), new FrameworkPropertyMetadata(OnHtmlChanged)); [AttachedPropertyBrowsableForType(typeof(WebBrowser))] public static string GetHtml(WebBrowser d) { return (string)d.GetValue(HtmlProperty); } public static void SetHtml(WebBrowser d, string value) { d.SetValue(HtmlProperty, value); } static void OnHtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { WebBrowser wb = d as WebBrowser; if (wb != null) wb.NavigateToString(e.NewValue as string); } } 

And you will use it like this (where lcl is the alias of xmlns-namespace-alias):

 <WebBrowser lcl:BrowserBehavior.Html="{Binding HtmlToDisplay}" /> 
+94
Apr 6 2018-10-06T00:
source share



All Articles