Skip to content

Instantly share code, notes, and snippets.

@mastry
Last active October 12, 2022 13:03
Show Gist options
  • Save mastry/ef209b424ed3a9548a757b72b6df5516 to your computer and use it in GitHub Desktop.
Save mastry/ef209b424ed3a9548a757b72b6df5516 to your computer and use it in GitHub Desktop.
Use this to data bind the Source property to a URL
static class WebBrowserBehaviors
{
public static readonly DependencyProperty BindableSourceProperty =
DependencyProperty.RegisterAttached(
"BindableSource",
typeof(string),
typeof(WebBrowserBehaviors),
new UIPropertyMetadata(null, BindableSourceChanged));
static void BindableSourceChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var webBrowser = sender as WebBrowser;
if (webBrowser == null) return;
var url = e.NewValue as string;
if (url != null)
{
webBrowser.Source = new Uri(url);
}
}
public static string GetBindableSource(DependencyObject obj)
{
return obj.GetValue(BindableSourceProperty) as string;
}
public static void SetBindableSource(DependencyObject obj, string value)
{
obj.SetValue(BindableSourceProperty, value);
}
}
<!-- In your window XAML file, add the attched behavior-->
<WebBrowser Behaviors:WebBrowserBehaviors.BindableSource="{Binding SelectedItem.Url}" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment