【发布时间】:2011-09-17 13:43:30
【问题描述】:
我正在查看this question,但我不明白如何实际使用创建的 AttachedProperty。问题是试图对 WebBrowser 控件的源进行绑定。
那里的代码如下:
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 = uri != null ? new Uri(uri) : null;
}
}
}
和
<WebBrowser ns:WebBrowserUtility.BindableSource="{Binding WebAddress}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
Width="300"
Height="200" />
网址,究竟是什么?这是我的理解(可能是错误的):
- 有一个 AttachedProperty 可以附加到任何对象,在这种特殊情况下,它基本上只是附加一个名为 BindableSource 的 String 类型的属性。
-
当我们拥有“{Binding WebAddress}”时,这意味着在处理此 .xaml 文件的某些 c# 代码中,有如下内容:
public String WebAddress { // get and set here? not sure } - 为了利用已更改的属性,我可以调用 RaisedPropertyChanged,它会在那里触发该静态方法?
即使我看着它,它似乎也不对,但我在网上找不到任何可以帮助我的东西。
【问题讨论】:
标签: c# wpf data-binding browser webbrowser-control