databind WPF中WebBrowser的Source属性

wpcxdonn  于 2023-03-24  发布在  其他
关注(0)|答案(6)|浏览(166)

有人知道如何在WPF(3.5SP1)中对WebBrowser的.Source属性进行数据绑定吗?我有一个列表视图,我希望在左侧有一个小的WebBrowser,在右侧有内容,并将每个WebBrowser的源代码与绑定到列表项的每个对象中的URI进行数据绑定。
这是我迄今为止的概念证明,但“<WebBrowser Source="{Binding Path=WebAddress}"”无法编译。

<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>
s6fujrry

s6fujrry1#

问题是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中做:

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

ygya80vv2#

我写了一个 Package 器usercontrol,它使用了DependencyProperties:
XAML:

<UserControl x:Class="HtmlBox">
    <WebBrowser x:Name="browser" />
</UserControl>

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

像这样使用它:

<Controls:HtmlBox HtmlText="{Binding MyHtml}"  />

唯一的问题是WebBrowser控件不是“纯粹的”wpf...它实际上只是一个win32组件的 Package 器。这意味着控件不会尊重z索引,并且总是覆盖其他元素(例如:在scrollviewer中,这可能会引起一些麻烦)有关MSDN上这些win32-wpf问题的更多信息

dwbf0jvd

dwbf0jvd3#

我稍微修改了托德的优秀答案,以生成一个版本,该版本可以处理来自Binding源代码的字符串或URI:

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

2wnc66cl4#

好主意托德
我已经做了类似的RichTextBox。选择。文本在Silverlight 4现在。感谢您的帖子。工程罚款。

public class RichTextBoxHelper
{
    public static readonly DependencyProperty BindableSelectionTextProperty =
       DependencyProperty.RegisterAttached("BindableSelectionText", typeof(string), 
       typeof(RichTextBoxHelper), new PropertyMetadata(null, BindableSelectionTextPropertyChanged));

    public static string GetBindableSelectionText(DependencyObject obj)
    {
        return (string)obj.GetValue(BindableSelectionTextProperty);
    }

    public static void SetBindableSelectionText(DependencyObject obj, string value)
    {
        obj.SetValue(BindableSelectionTextProperty, value);
    }

    public static void BindableSelectionTextPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        RichTextBox rtb = o as RichTextBox;
        if (rtb != null)
        {
            string text = e.NewValue as string;
            if (text != null)
                rtb.Selection.Text = text;
        }
    }
}

下面是Xaml-Code。

<RichTextBox IsReadOnly='False' TextWrapping='Wrap' utilities:RichTextBoxHelper.BindableSelectionText="{Binding Content}"/>
b09cbbtk

b09cbbtk5#

这是对托德和Samuel的答案的改进,利用了一些基本的逻辑前提,并使用了空合并运算符。

public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
    WebBrowser browser = o as WebBrowser;

    if ((browser != null) && (e.NewValue != null))
        browser.Source = e.NewValue as Uri ?? new Uri((string)e.NewValue);

}

1.如果浏览器为空或位置为空,则我们无法使用或导航到空页面。
1.当#1中的项不为空时,则在赋值时,如果新值是URI,则使用它。如果不是并且URI为空,则聚结它必须是可以放入URI的字符串;因为#1强制字符串不能为null。

zlwx9yxi

zlwx9yxi6#

您需要在指向类文件的xaml文件的前几行声明它

xmlns:reportViewer="clr-namespace:CoMS.Modules.Report"

相关问题