0

I'm using MVVM and want to bind a WebBrowser with my ViewModel(-property). I've used this method to do a bindable WebBrowser: http://pastebin.com/1SwwQgJZ

Anyway, I want to create dynamically a formular based on a XML file. My Application looks like this: The user opens the WPF-program and logs in with his account. After this, another View is being popped up, which contains a WebBrowser & the WPF contains a combobox with some XML files. The user can pick a XML file and based on it's content, the source of the WebBrowser will be renewed. I'd do it like this (just a snipped to show you how I'd create the HTML-file):

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'. 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.

Edit: I'm creating the HTML-file like this:

        _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'>";
4

1 回答 1

0

得到了我想要的。

可绑定的网络浏览器:

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

在虚拟机中

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();
        }
    }
}
于 2013-06-25T08:57:16.840 回答