Xaml Webbrowser动态创建

时间:2013-06-24 13:32:10

标签: c# html dynamic properties viewmodel

我正在使用MVVM并希望将WebBrowser与我的ViewModel(-property)绑定。我已经使用此方法来执行可绑定的WebBrowser:http://pastebin.com/1SwwQgJZ

无论如何,我想基于XML文件动态创建一个公式。我的应用程序如下所示:用户打开WPF程序并使用他的帐户登录。在此之后,另一个View正在弹出,其中包含一个WebBrowser& WPF包含一个带有一些XML文件的组合框。用户可以选择XML文件并根据其内容更新WebBrowser的源代码。我会这样做(只是一个剪辑,告诉你我如何创建HTML文件):

foreach(Question q in QuestionList)
    foreach(Answer a in AnswerList)
        _reportpage + = "<td> " + _reportpage + "</td>";

所以我想创建一个表单,后面看起来像这样:

                       good  middle  bad

How do you feel?       [ ]   [ ]     [X]
How was school?        [X]   [ ]     [ ]

[]是radiobuttons。创建这样的表单不是问题,问题是将数据发送回ViewModel ...我想知道用户点击了哪个答案并将其保存到“答案”列表中。我该怎么办呢?

由于我必须动态创建WPF表单并希望在WPF / WP8 / W8 / Silverlight上使用它,因此这种方法在我看来是最好的方法,因为所有平台都可以处理HTML。

编辑:我正在创建这样的HTML文件:

        _reportpage = "<table border='2'>";
        _reportpage += "<tr><td></td>";
        foreach (T_Answer answer in AnswerList)
        {
            _reportpage += "<td>" + answer.Text + "</td>";
        }
        _reportpage += "</tr>";
        foreach (T_Question tq in QuestionList)
        {
            _reportpage += "<tr><td>" + tq.Text + "</td>";
            foreach (T_Answer ta in tq.Answer)
            {
                _reportpage += "<td><input type='radio' name='" + tq.ID + "' value='" + ta.Answer.ID + "'>" + ta.Answer.Text + "</td>";
            }
            _reportpage += "</tr>";
        }
        _reportpage += "</table>";
        _reportpage += "<input type='button' value='Submit'>";

1 个答案:

答案 0 :(得分:0)

按照我的意愿得到它。

Bindable WebBrowser:

<WebBrowser html:WebBrowserUtility.BindableSource="{Binding ReportPage, Mode=TwoWay}" Name="webbrowser" />
在VM中

private string _reportpage;
public string ReportPage
{
    get
    {
        return _reportpage;
    }
    set
    {
        if (_reportpage != value)
        {
            _reportpage = value;
            RaisePropertyChanged(() => ReportPage);

        }
    }
}

ReportPage是HTML文件,它将动态创建,如下所示:

_reportpage = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><title>'" + title + "</title></head><body><h1>" + title + "</h1>...................";

代码背后:

private readonly MainViewModel _viewModel;
public MainWindow()
{
    InitializeComponent();
    webbrowser.LoadCompleted += new LoadCompletedEventHandler(webbrowser_LoadCompleted);
    _viewModel = DataContext as MainViewModel;
}

private void webbrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
        mshtml.HTMLDocument doc;
        doc = (mshtml.HTMLDocument)webbrowser.Document;
        mshtml.HTMLDocumentEvents2_Event iEvent;
        iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;
        iEvent.onclick += new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);
}

private bool ClickEventHandler(mshtml.IHTMLEventObj pEvtObj)
{
    _viewModel.HTMLObject = pEvtObj;
    return true;
}

现在我在ViewModel中有HTMLObject,我可以从HTML访问数据。

private mshtml.IHTMLEventObj _htmlobject;
public mshtml.IHTMLEventObj HTMLObject
{
    get
    {
        return _htmlobject;
    }
    set
    {
        _htmlobject = value;
        if (_htmlobject.srcElement.tagName == "INPUT" && _htmlobject.srcElement.getAttribute("type") == "radio")
        {
            string QuestionID = _htmlobject.srcElement.getAttribute("name");
            string AnswerID = _htmlobject.srcElement.getAttribute("defaultValue");
            //Foreach Question and foreach answer, check the ID with the ID's of the collections and do some stuff
        }
        else if (_htmlobject.srcElement.tagName == "INPUT" && _htmlobject.srcElement.getAttribute("type") == "button")
        {
            //I have only 1 button, so if the user clicks the button, formular should be send
            SendFormular();
        }
    }
}