[英]Xaml Webbrowser creating dynamically formular
我正在使用MVVM,並想將WebBrowser與ViewModel(-property)綁定。 我已經使用此方法來執行可綁定的WebBrowser: http : //pastebin.com/1SwwQgJZ
無論如何,我想基於XML文件動態創建公式器。 我的應用程序如下所示:用戶打開WPF程序並使用其帳戶登錄。 此后,將彈出另一個包含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] [ ] [ ]
[]是單選按鈕。 創建這樣的表單不是問題,問題是將數據發送回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'>";
如我所願。
可綁定的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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.