我目前正在开发一个简单的xml查看器控件来显示一些具有简单导航功能的xml文件,例如在Internet Explorer中打开xml文件时。
为此,我在msdn论坛中找到了一个很好的控件:
<UserControl x:Class="WpfApplication1.CustomControl.Viewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xmlstack="clr-namespace:System.Xml;assembly=System.Xml">
<UserControl.Resources>
<SolidColorBrush x:Key="xmlValueBrush" Color="Blue" />
<SolidColorBrush x:Key="xmAttributeBrush" Color="Red" />
<SolidColorBrush x:Key="xmlTagBrush" Color="DarkMagenta" />
<SolidColorBrush x:Key="xmlMarkBrush" Color="Blue" />
<DataTemplate x:Key="attributeTemplate">
<StackPanel Margin="3,0,0,0"
HorizontalAlignment="Center"
Orientation="Horizontal">
<TextBlock Foreground="{StaticResource xmAttributeBrush}" Text="{Binding Path=Name}" />
<TextBlock Foreground="{StaticResource xmlMarkBrush}" Text="="" />
<TextBlock Foreground="{StaticResource xmlValueBrush}" Text="{Binding Path=Value}" />
<TextBlock Foreground="{StaticResource xmlMarkBrush}" Text=""" />
</StackPanel>
</DataTemplate>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True" />
</Style>
<HierarchicalDataTemplate x:Key="treeViewTemplate" ItemsSource="{Binding XPath=child::node()}">
<StackPanel Margin="3,0,0,0"
HorizontalAlignment="Center"
Orientation="Horizontal">
<TextBlock x:Name="startTag"
HorizontalAlignment="Center"
Foreground="{StaticResource xmlMarkBrush}"
Text="<" />
<TextBlock x:Name="xmlTag"
Margin="0"
HorizontalAlignment="Center"
Foreground="{StaticResource xmlTagBrush}"
Text="{Binding Path=Name}" />
<ItemsControl HorizontalAlignment="Center"
ItemsSource="{Binding Path=Attributes}"
ItemTemplate="{StaticResource attributeTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<TextBlock x:Name="endTag"
HorizontalAlignment="Center"
Foreground="{StaticResource xmlMarkBrush}"
Text=">" />
</StackPanel>
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding NodeType}">
<DataTrigger.Value>
<xmlstack:XmlNodeType>Text</xmlstack:XmlNodeType>
</DataTrigger.Value>
<Setter TargetName="xmlTag" Property="Text" Value="{Binding InnerText}" />
<Setter TargetName="xmlTag" Property="Foreground" Value="Blue" />
<Setter TargetName="startTag" Property="Visibility" Value="Collapsed" />
<Setter TargetName="endTag" Property="Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding HasChildNodes}" Value="False">
<Setter TargetName="endTag" Property="Text" Value=" />" />
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
</UserControl.Resources>
<Grid>
<TreeView Name="xmlTree"
Grid.Row="2"
Grid.ColumnSpan="2"
ItemTemplate="{StaticResource treeViewTemplate}" />
</Grid>
</UserControl>
代码隐藏:
using System.Windows.Controls;
using System.Windows.Data;
using System.Xml;
namespace WpfApplication1.CustomControl
{
/// <summary>
/// Interaction logic for Viewer.xaml
/// </summary>
public partial class Viewer : UserControl
{
private XmlDocument _xmldocument;
public Viewer()
{
InitializeComponent();
}
public XmlDocument xmlDocument
{
get { return _xmldocument; }
set
{
_xmldocument = value;
BindXMLDocument();
}
}
private void BindXMLDocument()
{
if (_xmldocument == null)
{
xmlTree.ItemsSource = null;
return;
}
XmlDataProvider provider = new XmlDataProvider();
provider.Document = _xmldocument;
Binding binding = new Binding();
binding.Source = provider;
binding.XPath = "child::node()";
xmlTree.SetBinding(TreeView.ItemsSourceProperty, binding);
}
}
}
我现在的问题是我想显示子节点的结束标记,在下面标有星号的例子中:
<root>
<node>Test*</node>*
*</root>*
我不知道如何实现这一点,有什么好建议吗?
提前谢谢。