Databind Source property for WebBrowser in WPF

Does anyone know how to bind the .Source property to WebBrowser in WPF (3.5SP1)? I have a list that I want to have a small WebBrowser on the left and content on the right, as well as bind the source of each WebBrowser with a URI in each object associated with the list item.

This is what I have as proof of concept so far, but " <WebBrowser Source="{Binding Path=WebAddress}" " does not compile.

 <DataTemplate x:Key="dealerLocatorLayout" DataType="DealerLocatorAddress"> <StackPanel Orientation="Horizontal"> <!--Web Control Here--> <WebBrowser Source="{Binding Path=WebAddress}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" Width="300" Height="200" /> <StackPanel Orientation="Vertical"> <StackPanel Orientation="Horizontal"> <Label Content="{Binding Path=CompanyName}" FontWeight="Bold" Foreground="Blue" /> <TextBox Text="{Binding Path=DisplayName}" FontWeight="Bold" /> </StackPanel> <TextBox Text="{Binding Path=Street[0]}" /> <TextBox Text="{Binding Path=Street[1]}" /> <TextBox Text="{Binding Path=PhoneNumber}"/> <TextBox Text="{Binding Path=FaxNumber}"/> <TextBox Text="{Binding Path=Email}"/> <TextBox Text="{Binding Path=WebAddress}"/> </StackPanel> </StackPanel> </DataTemplate> 
+74
c # browser data-binding wpf xaml
Nov 04 '08 at 21:39
source share
7 answers

The problem is that WebBrowser.Source is not a DependencyProperty. One way to solve this problem would be to use the magic of AttachedProperty to provide this ability.

 public static class WebBrowserUtility { public static readonly DependencyProperty BindableSourceProperty = DependencyProperty.RegisterAttached("BindableSource", typeof(string), typeof(WebBrowserUtility), new UIPropertyMetadata(null, BindableSourcePropertyChanged)); public static string GetBindableSource(DependencyObject obj) { return (string) obj.GetValue(BindableSourceProperty); } public static void SetBindableSource(DependencyObject obj, string value) { obj.SetValue(BindableSourceProperty, value); } public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { WebBrowser browser = o as WebBrowser; if (browser != null) { string uri = e.NewValue as string; browser.Source = !String.IsNullOrEmpty(uri) ? new Uri(uri) : null; } } } 

Then in your xaml do:

 <WebBrowser ns:WebBrowserUtility.BindableSource="{Binding WebAddress}"/> 
+135
Nov 05 '08 at 16:12
source share
— -

I wrote a usercontrol wrapper that uses DependencyProperties:

XAML:

 <UserControl x:Class="HtmlBox"> <WebBrowser x:Name="browser" /> </UserControl> 

FROM#:

 public static readonly DependencyProperty HtmlTextProperty = DependencyProperty.Register("HtmlText", typeof(string), typeof(HtmlBox)); public string HtmlText { get { return (string)GetValue(HtmlTextProperty); } set { SetValue(HtmlTextProperty, value); } } protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { base.OnPropertyChanged(e); if (e.Property == HtmlTextProperty) { DoBrowse(); } } private void DoBrowse() { if (!string.IsNullOrEmpty(HtmlText)) { browser.NavigateToString(HtmlText); } } 

and use it like this:

 <Controls:HtmlBox HtmlText="{Binding MyHtml}" /> 

The only problem with this is that the WebBrowser control is not a "clean" wpf ... in fact it is just a wrapper for the win32 component. This means that the control will not respect the z-index and will always overlap another element (for example: in scrollviewer this may cause some problems) more information about these win32-wpf problems on MSDN

+26
Jun 10 '09 at 15:19
source share

I got a little better with Todd to create a version that handles any strings or Uris from the Binding source:

 public static class WebBrowserBehaviors { public static readonly DependencyProperty BindableSourceProperty = DependencyProperty.RegisterAttached("BindableSource", typeof(object), typeof(WebBrowserBehaviors), new UIPropertyMetadata(null, BindableSourcePropertyChanged)); public static object GetBindableSource(DependencyObject obj) { return (string)obj.GetValue(BindableSourceProperty); } public static void SetBindableSource(DependencyObject obj, object value) { obj.SetValue(BindableSourceProperty, value); } public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { WebBrowser browser = o as WebBrowser; if (browser == null) return; Uri uri = null; if (e.NewValue is string ) { var uriString = e.NewValue as string; uri = string.IsNullOrWhiteSpace(uriString) ? null : new Uri(uriString); } else if (e.NewValue is Uri) { uri = e.NewValue as Uri; } browser.Source = uri; } 
+25
Jul 07 '11 at 15:55
source share

Cool idea Todd.

I have done the same with RichTextBox.Selection.Text in Silverlight 4 now. Thank you for message. It works great.

 public class RichTextBoxHelper { public static readonly DependencyProperty BindableSelectionTextProperty = DependencyProperty.RegisterAttached("BindableSelectionText", typeof(string), typeof(RichTextBoxHelper), new PropertyMetadata(null, BindableSelectionTextPropertyChanged)); public static string GetBindableSelectionText(DependencyObject obj) { return (string)obj.GetValue(BindableSelectionTextProperty); } public static void SetBindableSelectionText(DependencyObject obj, string value) { obj.SetValue(BindableSelectionTextProperty, value); } public static void BindableSelectionTextPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { RichTextBox rtb = o as RichTextBox; if (rtb != null) { string text = e.NewValue as string; if (text != null) rtb.Selection.Text = text; } } } 

Here is the Xaml-Code.

 <RichTextBox IsReadOnly='False' TextWrapping='Wrap' utilities:RichTextBoxHelper.BindableSelectionText="{Binding Content}"/> 
+3
May 7 '10 at
source share

You can also use a special separate proxy control . It applies not only to the WebBrowser case, but to any such control.

+1
Dec 16 '09 at 10:52
source share

This is a refinement for Todd and Samuel to use some basic logical prerequisites, as well as use the zero coalescence operator.

 public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { WebBrowser browser = o as WebBrowser; if ((browser != null) && (e.NewValue != null)) browser.Source = e.NewValue as Uri ?? new Uri((string)e.NewValue); } 
  • If the browser is NULL or its value is null, we cannot use or go to the zero page.
  • When the elements in # 1 are not null, then when assigning, if the new value is a URI, use it. If not, and the URI is NULL, then it is concatenated because it must be a string that can be placed in the URI; since # 1 states that the string cannot be null.
0
May 20 '16 at 19:59
source share

You need to declare it in the first few lines of the xaml file, which points to the class file

 xmlns:reportViewer="clr-namespace:CoMS.Modules.Report" 
-3
Nov 15 '12 at 23:33
source share



All Articles