当前位置:  开发笔记 > 编程语言 > 正文

在WPF中数据绑定WebBrowser的Source属性

如何解决《在WPF中数据绑定WebBrowser的Source属性》经验,为你挑选了3个好方法。

有谁知道如何在WPF(3.5SP1)中数据绑定WebBrowser的.Source属性?我有一个listview,我希望在左边有一个小的WebBrowser,在右边有内容,并在每个WebBrowser的源中为每个绑定到列表项的对象中的URI进行数据绑定.

这是我迄今为止的概念证明,但" "不编译.

                
    
         
        
        
            
                
            
            
            
            
            
            
        
    

Todd White.. 154

问题是WebBrowser.Source不是DependencyProperty.一种解决方法是使用一些AttachedProperty魔法来启用此功能.

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;
        }
    }

}

然后在你的xaml做:


在"new Uri(uri)"上获得一个例外,因为字符串是"".也许应该是.... browser.Source = string.IsNullOrEmpty(uri)?null:new Uri(uri); (9认同)

请注意,这只是一种单向绑定,并且当Web浏览器页面更改时,BindableSource属性不会更改. (4认同)


Samuel Jack.. 33

我已经修改了Todd的优秀答案,以生成一个版本来处理绑定源中的字符串或Uris:

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;
    }


RoelF.. 30

我写了一个包装器usercontrol,它使用了DependencyProperties:

XAML:


    

C#:

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);
    }
}

并像这样使用它:


这个问题的唯一问题是WebBrowser控件不是"纯粹的"wpf ......它实际上只是win32组件的包装器.这意味着控件将不会尊重z-index,并且将始终覆盖其他元素(例如:在滚动查看器中这可能会导致一些问题)有关MSDN上这些win32-wpf问题的更多信息



1> Todd White..:

问题是WebBrowser.Source不是DependencyProperty.一种解决方法是使用一些AttachedProperty魔法来启用此功能.

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;
        }
    }

}

然后在你的xaml做:



在"new Uri(uri)"上获得一个例外,因为字符串是"".也许应该是.... browser.Source = string.IsNullOrEmpty(uri)?null:new Uri(uri);
请注意,这只是一种单向绑定,并且当Web浏览器页面更改时,BindableSource属性不会更改.

2> Samuel Jack..:

我已经修改了Todd的优秀答案,以生成一个版本来处理绑定源中的字符串或Uris:

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;
    }



3> RoelF..:

我写了一个包装器usercontrol,它使用了DependencyProperties:

XAML:


    

C#:

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);
    }
}

并像这样使用它:


这个问题的唯一问题是WebBrowser控件不是"纯粹的"wpf ......它实际上只是win32组件的包装器.这意味着控件将不会尊重z-index,并且将始终覆盖其他元素(例如:在滚动查看器中这可能会导致一些问题)有关MSDN上这些win32-wpf问题的更多信息

推荐阅读
有风吹过best
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有