Home > Back-end >  Why did my Browser Element doesent show my html string?
Why did my Browser Element doesent show my html string?

Time:11-11

So I try to work the first time with an Web Browser Element in Wpfl. I tryd to show my html string in the Web Browser Element. I tried it like that:

  public static readonly DependencyProperty HtmlProperty = DependencyProperty.RegisterAttached(
        "Html",
        typeof(string),
        typeof(Class1),
        new FrameworkPropertyMetadata(OnHtmlChanged));

    [AttachedPropertyBrowsableForType(typeof(WebBrowser))]
    public static string GetHtml(WebBrowser d)
    {
        return (string)d.GetValue(HtmlProperty);
    }

    public static void SetHtml(WebBrowser d, string value)
    {
        d.SetValue(HtmlProperty, value);
    }

    static void OnHtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        WebBrowser wb = d as WebBrowser;
        if (wb != null)
            wb.NavigateToString(e.NewValue as string);
    }

and in my View my Property like that:

private string _html;
    public string html1
    {
        get
        {
            string html = "html string here";
            return _html   html;
        }
        set
        {
            _html = value;
            NotifyPropertyChanged(nameof(html1));
        }
    }

and my XAML like that:

        <WebBrowser local:Class1.Html="{Binding html}"></WebBrowser>

But it dosent show my html string. What am I doing wrong?

CodePudding user response:

If it works when you set the attached property to a local value like this you know that your binding to html fails:

<WebBrowser local:Class1.Html="test..."></WebBrowser>

Also, you bind to a property called "html" but the one you have posted is named html1.

Ensure that you have set the DataContext of the control to a class that has a public html property that returns a string. Then your code should work.

  • Related