90

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

6 に答える 6

163

問題は、でWebBrowser.SourceはないということDependencyPropertyです。AttachedProperty回避策の1つは、この機能を有効にするために魔法を使用することです。

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}"/>
于 2008-11-05T16:12:57.403 に答える
33

Todd の優れた回答を少し修正して、Binding ソースの文字列または 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;
    }
于 2011-07-07T15:55:10.350 に答える
32

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 コンポーネントの単なるラッパーです。これは、コントロールが z-index を尊重せず、常に他の要素をオーバーレイすることを意味します (例: スクロールビューアーでは、これにより問題が発生する可能性があります) MSDNのこれらの win32-wpf の問題に関する詳細情報

于 2009-06-10T15:19:32.507 に答える
3

クールなアイデアのトッド。

Silverlight 4 の RichTextBox.Selection.Text でも同様のことを行いました。ご投稿ありがとうございます。正常に動作します。

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コードがあります。

<RichTextBox IsReadOnly='False' TextWrapping='Wrap' utilities:RichTextBoxHelper.BindableSelectionText="{Binding Content}"/>
于 2010-05-07T21:03:24.760 に答える
0

これは、トッドとサミュエルの答えを改良して、いくつかの基本的な論理前提を利用し、ヌル合体演算子を使用する..

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. ブラウザーが null または場所が null の場合、null ページを使用したり、そこに移動したりすることはできません。
  2. #1 の項目が null でない場合、割り当て時に、新しい値が URI の場合はそれを使用します。そうではなく、URI が null の場合、合体は URI に入れることができる文字列でなければなりません。#1は、文字列をnullにできないことを強制するためです。
于 2016-05-20T19:59:21.933 に答える
-3

xamlクラスファイルを指しているファイル の最初の数行で宣言する必要があります

xmlns:reportViewer="clr-namespace:CoMS.Modules.Report" 
于 2012-11-15T23:33:42.450 に答える