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?
Ok, so you are trying to implement a tree view, which have extension points, to have custom coloring / headers for xml nodes.
For this, you have added several public delegates for each XMLTreeViewItem, which caller may override to provide their own color/header/etc.
The problem with current solution is that only root node gets custom delegate for coloring. When root xml node is constructed, all children are loaded by constructing new XMLTreeViewItem, which have default implementation of LoadColor delegates.
You need to either copy delegate overrides into each new created node, or hold reference to root node, which have overridden delegates.
Another problem is that entire XMLTreeViewItem tree is generated in constructor, and delegate overrides are supplied only afterwards:
It means that at the time you are doing _LoadColor.Invoke, entire tree is already constructed and has its colors initialized. _LoadColor = new LoadColorDelegate would overwrite delegate you have passed to all children, And _LoadColor.Invoke would only color root node.
How I suggest to fix this:
Move LoadHeader / CheckForChildren / LoadColor methods (which you want to allow to override) to XMLTreeView class, and expose as public properties there:
Modify constructor of XMLTreeViewItem to accept instance of XMLTreeView, and store parent reference in each node:
Now, construct tree like this:
Alternative solution would be to pass new coloring/header delegates directly into XMLTreeViewItem constructor, and pass recursively into all lower nodes. This would keep XMLTreeView clean of coloring delegates, but probably would complicate customization for users of this class, since they would need to overwrite delegate in each node.