我可以绑定HTML到WPF Web浏览器控件? [英] Can I bind HTML to a WPF Web Browser Control?

查看:294
本文介绍了我可以绑定HTML到WPF Web浏览器控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个属性HTML(字符串)。我可以将绑定到一个WPF WebBrowser控件?有一个地方,我需要一个URI Source属性,但如果我在记忆我想呈现有一个HTML字符串,我能做到这一点?我使用MVVM,所以我认为它很难使用像 webBrowser1.NavigateToString方法()等?因为我不知道该控件的名称?

Suppose I have a property HTML (string). Can I bind that to a WPF WebBrowser control? There is the Source property where I need a URI, but if I have a HTML string in memory I want to render, can I do that? I am using MVVM, so I think its harder to use methods like webBrowser1.NavigateToString() etc? cos I won't know the control name?

推荐答案

请参阅this问题。

要总结一下,首先创建一个web浏览器附加属性。

To summarize, first you create an Attached Property for WebBrowser

public class BrowserBehavior
{
    public static readonly DependencyProperty HtmlProperty = DependencyProperty.RegisterAttached(
            "Html",
            typeof(string),
            typeof(BrowserBehavior),
            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 dependencyObject, DependencyPropertyChangedEventArgs e)
    {
        WebBrowser webBrowser = dependencyObject as WebBrowser;
        if (webBrowser != null)
            webBrowser.NavigateToString(e.NewValue as string ?? " ");
    }
}

然后你可以绑定到你的HTML字符串,NavigateToString将被调用每次你的HTML字符串修改

And then you can Bind to your html string and NavigateToString will be called everytime your html string changes

<WebBrowser local:BrowserBehavior.Html="{Binding MyHtmlString}" />

这篇关于我可以绑定HTML到WPF Web浏览器控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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