在WPF中数据绑定WebBrowser的Source属性 [英] databind the Source property of the WebBrowser in WPF

查看:417
本文介绍了在WPF中数据绑定WebBrowser的Source属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何在WPF(3.5SP1)中数据绑定WebBrowser的.Source属性?
我有一个列表视图,我想要在左边有一个小的WebBrowser,右边的内容和每个WebBrowser的源数据绑定,每个对象的URI绑定到列表项。



这是我迄今为止的概念证明,但是< WebBrowser Source ={Binding Path = WebAddress}不编译。

 < DataTemplate x:Key =dealerLocatorLayoutDataType =DealerLocatorAddress> 
< StackPanel Orientation =Horizo​​ntal>
<! - Web Control Here - >
< WebBrowser Source ={Binding Path = WebAddress}
ScrollViewer.Horizo​​ntalScrollBarVisibility =禁用
ScrollViewer.VerticalScrollBarVisibility =禁用
宽度=300
Height =200
/>
< StackPanel Orientation =Vertical>
< StackPanel Orientation =Horizo​​ntal>
< Label Content ={Binding Path = CompanyName}FontWeight =BoldForeground =Blue/>
< TextBox Text ={Binding Path = DisplayName}FontWeight =Bold/>
< / StackPanel>
< TextBox Text ={Binding Path = Street [0]}/>
< TextBox Text ={Binding Path = Street [1]}/>
< TextBox Text ={Binding Path = PhoneNumber}/>
< TextBox Text ={Binding Path = FaxNumber}/>
< TextBox Text ={Binding Path = Email}/>
< TextBox Text ={Binding Path = WebAddress}/>
< / StackPanel>
< / StackPanel>
< / DataTemplate>


解决方案

问题是 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作为WebBrowser;
if(browser!= null)
{
string uri = e.NewValue as string;
browser.Source =!String.IsNullOrEmpty(uri)?新Uri(uri):null;
}
}

}

然后在你的xaml做:

 < WebBrowser ns:WebBrowserUtility.BindableSource ={Binding WebAddress}/> 


Does anyone know how to databind the .Source property of the WebBrowser in WPF ( 3.5SP1 )? I have a listview that I want to have a small WebBrowser on the left, and content on the right, and to databind the source of each WebBrowser with the URI in each object bound to the list item.

This is what I have as a proof of concept so far, but the "<WebBrowser Source="{Binding Path=WebAddress}"" does not compile.

<DataTemplate x:Key="dealerLocatorLayout" DataType="DealerLocatorAddress">                
    <StackPanel Orientation="Horizontal">
         <!--Web Control Here-->
        <WebBrowser Source="{Binding Path=WebAddress}"
            ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
            ScrollViewer.VerticalScrollBarVisibility="Disabled" 
            Width="300"
            Height="200"
            />
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <Label Content="{Binding Path=CompanyName}" FontWeight="Bold" Foreground="Blue" />
                <TextBox Text="{Binding Path=DisplayName}" FontWeight="Bold" />
            </StackPanel>
            <TextBox Text="{Binding Path=Street[0]}" />
            <TextBox Text="{Binding Path=Street[1]}" />
            <TextBox Text="{Binding Path=PhoneNumber}"/>
            <TextBox Text="{Binding Path=FaxNumber}"/>
            <TextBox Text="{Binding Path=Email}"/>
            <TextBox Text="{Binding Path=WebAddress}"/>
        </StackPanel>
    </StackPanel>
</DataTemplate>

解决方案

The problem is that WebBrowser.Source is not a DependencyProperty. One workaround would be to use some AttachedProperty magic to enable this ability.

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

}

Then in your xaml do:

<WebBrowser ns:WebBrowserUtility.BindableSource="{Binding WebAddress}"/>

这篇关于在WPF中数据绑定WebBrowser的Source属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆