您可以创建一个Attached Property
为您执行此操作:
public static class WebBrowserProperties
{
public static readonly DependencyProperty UrlProperty = DependencyProperty.RegisterAttached("Url", typeof(string), typeof(WebBrowserProperties), new UIPropertyMetadata(string.Empty, UrlPropertyChanged));
public static string GetUrl(DependencyObject dependencyObject)
{
return (string)dependencyObject.GetValue(UrlProperty);
}
public static void SetUrl(DependencyObject dependencyObject, string value)
{
dependencyObject.SetValue(UrlProperty, value);
}
public static void UrlPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
WebBrowser webBrowser = dependencyObject as WebBrowser;
if (webBrowser != null && GetUrl(webBrowser) != string.Empty)
{
webBrowser.Navigate(GetUrl(webBrowser));
webBrowser.LoadCompleted += WebBrowserLoaded;
}
}
public static void WebBrowserLoaded(object sender, NavigationEventArgs e)
{
}
}
然后你可以像这样使用它:
<WebBrowser Attached:WebBrowserProperties.Url="{Binding YourUrlProperty}" />
要更新内容,只需更改YourUrlProperty
属性的值。