Reputation: 1939
In the Winform, i have a UserControl TreeView and It loads real time data from XML file. The XML files loaded successfully in the treeView.
I want to generate TreeView with Different images for different sets of data. This link explains to generate treeview for specific array of data. [http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.imagelist][1]
How can I add different images for each parent and child nodes, in the XML, I want to add different images for Global
Files
Section
and for Data
. Please explain to me with some snippet.
<Global>
<Files name="Bit_RunvsDepth" >
<Section name="Linguini">
<Data>measured depth</Data>
</Section>
<Section name="Process">
<Data>Tree</Data>
<Section name="Arguments">
<Data>None</Data>
</Section>
<Section name="Extras">
<Data>0.01</Data>
<Data>Foodg</Data>
</Section>
</Section>
<Section name="Color">
<Data>0.0</Data>
</Section>
<Section name="MinScale">
<Data>0</Data>
</Section>
<Section name="MaxScale">
<Data>1000</Data>
</Section>
</Files>
</Global>
Upvotes: 1
Views: 1196
Reputation: 6882
TreeNode class is not sealed, so you can build a hierarhy of custom node types.
abstract class CustomTreeDataNode : TreeNode
{
public CustomTreeDataNode()
{
}
protected void ReadChildNodes<T>(XmlNode parent, string childNodeName)
where T: CustomTreeDataNode, new()
{
foreach(XmlNode node in parent.SelectNodes(childNodeName))
{
T item = new T();
item.Fill(node);
Nodes.Add(item);
}
}
public void Fill(XmlNode node)
{
Nodes.Clear();
InitProperties(node);
}
protected abstract void InitProperties(XmlNode node);
}
class RootNode : CustomTreeDataNode
{
protected override void InitProperties(XmlNode source)
{
Text = "Root";
ItemIndex = ROOT_ITEMINDEX;
SelectedIndex = ROOT_SELECTEDINDEX;
ReadChildNodes<FileNode>(source, "Files"));
}
}
class FileNode : CustomTreeDataNode
{
protected override void InitProperties(XmlNode source)
{
Text = source["name"];
ItemIndex = FILE_ITEMINDEX;
SelectedIndex = FILE_SELECTEDINDEX;
ReadChildNodes<SectionNode>(source, "Section"));
}
}
class SectionNode : CustomTreeDataNode
{
protected override void InitProperties(XmlNode source)
{
Text = source["name"];
ItemIndex = SECTION_ITEMINDEX;
SelectedIndex = SECTION_SELECTEDINDEX;
ReadChildNodes<DataNode>(source, "Data"));
}
}
class DataNode : CustomTreeDataNode
{
protected override void InitProperties(XmlNode source)
{
Text = source.Text;
ItemIndex = DATA_ITEMINDEX;
SelectedIndex = DATA_SELECTEDINDEX;
}
}
...
RootNode root = new RootNode();
root.Fill(rootXmlNode);
treeView1.Nodes.Add(root);
To draw images TreeView relies on ImageView component. This link explains how to load images programmatically
Upvotes: 1