Last active
October 12, 2022 13:03
-
-
Save mastry/ef209b424ed3a9548a757b72b6df5516 to your computer and use it in GitHub Desktop.
Use this to data bind the Source property to a URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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