I'm creating a XML-viewer which should be able to read every XML file and put it in a treeview. My goal is to create a XMLViewer control, an user should be able to change certain routines in his own implementations. I provide default implementations that offer a basic functionality so that the XML viewer at least shows a default behaviour. I'm trying to do this with plumbing and delegates.
What I have so far:
MainWindow.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>
MainWindow.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();
}
}
Class 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);
}
}
}
}
Class 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
My goal:
http://i46.tinypic.com/29uua83.png
As you can see I have trouble to display my Treenodes correct. Does anyone have an idea to fix this?