我正在创建一个 XML 查看器,它应该能够读取每个 XML 文件并将其放入树视图中。我的目标是创建一个 XMLViewer 控件,用户应该能够在他自己的实现中更改某些例程。我提供了提供基本功能的默认实现,以便 XML 查看器至少显示默认行为。我正在尝试通过管道和代表来做到这一点。
到目前为止我所拥有的:
主窗口.xaml
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30*" />
<RowDefinition Height="25*" />
<RowDefinition Height="175*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="0,5,0,5">
<TextBlock Text="XML File" VerticalAlignment="Center" />
<TextBox Name="txtPath" Width="400" IsReadOnly="True" Margin="5,0,5,0"/>
<Button Content="Open" Name="btnOpen" />
</StackPanel>
<Button Name="btnPlumb" Content="Plumb the code!" Grid.Row="1"/>
<uc:XMLTreeView x:Name="XMLOutput" Grid.Row="2" />
</Grid>
主窗口.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//Events
btnOpen.Click += new RoutedEventHandler(ClickedOnOpen);
btnPlumb.Click += new RoutedEventHandler(ClickedOnPlumb);
}
private void ClickedOnPlumb(object sender, RoutedEventArgs e)
{
plumbCode();
}
private void ClickedOnOpen(object sender, RoutedEventArgs e)
{
selectXMLFile();
}
private void selectXMLFile()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "XML-Files |*.xml";
ofd.InitialDirectory = "C:\\";
if (ofd.ShowDialog() == true)
{
string path = ofd.FileName;
txtPath.Text = path;
XMLOutput.processXML(path);
}
}
private void plumbCode()
{
XMLOutput.PlumbTheCode();
}
}
类 XMLTreeView
namespace XMLViewer
{
class XMLTreeView : TreeView
{
public XmlDocument doc;
public void processXML(string path)
{
XmlDocument document = new XmlDocument();
this.doc = document;
doc.Load(path);
foreach (XmlNode node in doc.ChildNodes)
{
XMLTreeViewItem newItem = new XMLTreeViewItem(node);
this.AddChild(newItem);
}
}
public void PlumbTheCode()
{
this.Items.Clear();
foreach (XmlNode node in doc.ChildNodes)
{
XMLTreeViewItem newItem;
newItem = new XMLTreeViewItem(node);
newItem._LoadColor = new LoadColorDelegate(newItem.LoadColorPlumbed);
newItem._LoadColor.Invoke(node);
this.AddChild(newItem);
}
}
}
}
类 XMLTreeViewItem
namespace XMLViewer
{
public delegate void LoadHeaderDelegate(XmlNode node);
public delegate void LoadColorDelegate(XmlNode node);
public delegate void CheckForChildrenDelegate(XmlNode node);
public class XMLTreeViewItem:TreeViewItem
{
public LoadHeaderDelegate _LoadHeader { get; set; }
public LoadColorDelegate _LoadColor { get; set; }
public CheckForChildrenDelegate _CheckForChildren { get; set; }
public XMLTreeViewItem(XmlNode node)
{
_LoadHeader = new LoadHeaderDelegate(LoadHeader);
_LoadColor = new LoadColorDelegate(LoadColor);
_CheckForChildren = new CheckForChildrenDelegate(CheckForChildren);
_LoadHeader.Invoke(node);
_LoadColor.Invoke(node);
_CheckForChildren.Invoke(node);
}
#region HEADER
private void LoadHeader(XmlNode RootNode)
{
if (RootNode.HasChildNodes == false)
{
this.Header = RootNode.InnerText.ToUpper();
}
else
{
this.Header = RootNode.Name.ToUpper();
}
if (RootNode.Attributes != null)
{
foreach (XmlAttribute attr in RootNode.Attributes)
{
this.Header += " " + attr.Name + " = " + attr.InnerText;
}
}
}
#endregion
#region COLOR
private void LoadColor(XmlNode node)
{
this.Foreground = Brushes.Black;
}
public void LoadColorPlumbed(XmlNode node)
{
this.Foreground = Brushes.Green;
}
#endregion
#region CHILDREN
private void CheckForChildren(XmlNode node)
{
if (node.HasChildNodes)
{
LoadChildren(node);
}
}
private void LoadChildren(XmlNode RootNode)
{
foreach (XmlNode node in RootNode.ChildNodes)
{
XMLTreeViewItem newItem = new XMLTreeViewItem(node);
this.AddChild(newItem);
}
}
#endregion
}
}
http://oi47.tinypic.com/34o94cw.jpg
我的目标:
http://i46.tinypic.com/29uua83.png
如您所见,我无法正确显示我的 Treenodes。有没有人有解决这个问题的想法?