Я решил это с помощью элемента управления WebBrowser
.
Я не нашел хорошего решения с TreeView
, но я полностью удовлетворен этим кодом, потому что он полный MVVM:
Создайте свойство зависимостей для элементов управления WebBrowser с именем BindableXmlData
:
Public Class AttachedWebBrowserProperties
#Region "BindableXmlDataProperty"
Public Shared ReadOnly BindableXmlDataProperty As DependencyProperty = _
DependencyProperty.RegisterAttached("BindableXmlData", _
GetType(String), _
GetType(AttachedWebBrowserProperties), _
New UIPropertyMetadata(Nothing, AddressOf BindableXmlDataPropertyChanged))
Public Shared Function GetBindableXmlData(ByVal obj As DependencyObject) As ICommand
Return CType(obj.GetValue(BindableXmlDataProperty), ICommand)
End Function
Public Shared Sub SetBindableXmlData(ByVal obj As DependencyObject, ByVal value As String)
obj.SetValue(BindableXmlDataProperty, value)
End Sub
Public Shared Sub BindableXmlDataPropertyChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim browser As WebBrowser = TryCast(d, WebBrowser)
If browser IsNot Nothing Then
Dim xmldata As String = TryCast(e.NewValue, String)
If xmldata IsNot Nothing Then
browser.NavigateToString(xmldata)
End If
End If
End Sub
#End Region
End Class
Затем добавьте новый файл к вашему решению с именем XmlToHtmlStylesheet.xslt
в качестве встроенного ресурса со следующим содержимым:
<!--
|
| XSLT REC Compliant Version of IE5 Default Stylesheet
|
| Original version by Jonathan Marsh (jmarsh@xxxxxxxxxxxxx)
| http://msdn.microsoft.com/xml/samples/defaultss/defaultss.xsl
|
| Conversion to XSLT 1.0 REC Syntax by Steve Muench (smuench@xxxxxxxxxx)
|
+-->
<![CDATA[
function f(e){
if (e.className=="ci") {
if (e.children(0).innerText.indexOf("\n")>0) fix(e,"cb");
}
if (e.className=="di") {
if (e.children(0).innerText.indexOf("\n")>0) fix(e,"db");
} e.id="";
}
function fix(e,cl){
e.className=cl;
e.style.display="block";
j=e.parentElement.children(0);
j.className="c";
k=j.children(0);
k.style.visibility="visible";
k.href="#";
}
function ch(e) {
mark=e.children(0).children(0);
if (mark.innerText=="+") {
mark.innerText="-";
for (var i=1;i
BODY {font:x-small 'Verdana'; margin-right:1.5em}
.c {cursor:hand}
.b {color:red; font-family:'Courier New'; font-weight:bold;
text-decoration:none}
.e {margin-left:1em; text-indent:-1em; margin-right:1em}
.k {margin-left:1em; text-indent:-1em; margin-right:1em}
.t {color:#990000}
.xt {color:#990099}
.ns {color:red}
.dt {color:green}
.m {color:blue}
.tx {font-weight:bold}
.db {text-indent:0px; margin-left:1em; margin-top:0px;
margin-bottom:0px;padding-left:.3em;
border-left:1px solid #CCCCCC; font:small Courier}
.di {font:small Courier}
.d {color:blue}
.pi {color:blue}
.cb {text-indent:0px; margin-left:1em; margin-top:0px;
margin-bottom:0px;padding-left:.3em; font:small Courier;
color:#888888}
.ci {font:small Courier; color:#888888}
PRE {margin:0px; display:inline}
nbsp
<?
?>
nbsp
<?
xml
="
"
?>
x
t
="
"
-
<!--
NBSP-> f (чистый); NBSP <</SPAN>
ИксT/> - <</SPAN>
ИксT>
NBSP<</SPAN>
ИксT> </ </SPAN>
ИксT> - <</SPAN>
ИксT>
&;
Создайте новое окно (или в моем случае пользовательский контроль):
<UserControl x:Class="AdditionalXmlView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="clr-namespace:MyApplication.Framework">
<WebBrowser ui:AttachedWebBrowserProperties.BindableXmlData="{Binding XmlData}" />
</UserControl>
Создайте видовую модель, подобную этой:
Imports System.Xml
Imports System.Xml.Xsl
Imports System.Text
Imports System.IO
Imports System.ComponentModel
Public Class AdditionalXmlViewModel
Implements INotifyPropertyChanged
Private _XmlData As String
Public Property XmlData() As String
Get
Return _XmlData
End Get
Set(ByVal value As String)
_XmlData = value
OnPropertyChanged("XmlData")
End Set
End Property
Public Sub LoadXmlData(ByVal xmlDocument As XmlDocument)
Dim sb As New StringBuilder(2500)
Dim xslt As New XslCompiledTransform()
Dim stylesheetStream As Stream = GetType(AdditionalXmlView).Assembly.GetManifestResourceStream("MyApplication.Framework.XmlToHtmlStylesheet.xslt")
If stylesheetStream IsNot Nothing Then
Dim xmlReader As XmlReader = xmlReader.Create(stylesheetStream)
xslt.Load(xmlReader)
Dim settings As New XmlWriterSettings()
Dim dest As XmlWriter = XmlWriter.Create(sb, settings)
xslt.Transform(xmlDocument, dest)
End If
XmlData = sb.ToString()
End Sub
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Public Sub OnPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
Создайте свой види просмотрите модель и установите xmldata:
Public Class XYZ
Private additionalView As AdditionalXmlView
Private additionalViewModel As AdditionalXmlViewModel
Private Sub CreateInstances()
additionalViewModel = New AdditionalXmlViewModel()
additionalView = New AdditionalXmlView()
additionalView.DataContext = additionalViewModel
End Sub
Private Sub SetXmlDataOfAdditionalView()
Dim xmlDocument As New XmlDocument()
xmlDocument.Load("myXmlFile.xml")
additionalViewModel.LoadXmlData(xmlDocument)
End Sub
End Class