简体   繁体   English

Xaml Webbrowser动态创建公式器

[英]Xaml Webbrowser creating dynamically formular

I'm using MVVM and want to bind a WebBrowser with my ViewModel(-property). 我正在使用MVVM,并想将WebBrowser与ViewModel(-property)绑定。 I've used this method to do a bindable WebBrowser: http://pastebin.com/1SwwQgJZ 我已经使用此方法来执行可绑定的WebBrowser: http : //pastebin.com/1SwwQgJZ

Anyway, I want to create dynamically a formular based on a XML file. 无论如何,我想基于XML文件动态创建公式器。 My Application looks like this: The user opens the WPF-program and logs in with his account. 我的应用程序如下所示:用户打开WPF程序并使用其帐户登录。 After this, another View is being popped up, which contains a WebBrowser & the WPF contains a combobox with some XML files. 此后,将弹出另一个包含WebBrowser的视图,而WPF包含带有一些XML文件的组合框。 The user can pick a XML file and based on it's content, the source of the WebBrowser will be renewed. 用户可以选择一个XML文件,并根据其内容更新WebBrowser的源。 I'd do it like this (just a snipped to show you how I'd create the HTML-file): 我会这样做(只是向您展示了如何创建HTML文件的片段):

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

So I want to create a form, which looks later like this: 所以我想创建一个表单,稍后看起来像这样:

                       good  middle  bad

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

The [ ] are radiobuttons. []是单选按钮。 Creating a form like this isn't the problem, the problem is to send the data back to the ViewModel... I'd like to know which answer the user clicked and save it to a list of 'Answer'. 创建这样的表单不是问题,问题是将数据发送回ViewModel ...我想知道用户单击了哪个答案并将其保存到“答案”列表中。 How am I supposed to do it? 我应该怎么做?

Since I have to create dynamically a WPF-form and want to use this on WPF/WP8/W8/Silverlight, this method is in my eyes the best approach, since all the platforms can handle HTML. 由于我必须动态创建WPF表单并想在WPF / WP8 / W8 / Silverlight上使用它,因此在我看来,此方法是最好的方法,因为所有平台都可以处理HTML。

Edit: I'm creating the HTML-file like this: 编辑:我正在创建这样的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'>";

Got it as I wanted. 如我所愿。

Bindable WebBrowser: 可绑定的WebBrowser:

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

in VM 在VM中

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

        }
    }
}

ReportPage is the HTML File, which will be created dynamically, so something like this: 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>...................";

In Code Behind: 在后面的代码中:

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

Now I have the HTMLObject in my ViewModel and I can access the data from the HTML. 现在,我的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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM