C#:在UserControl WinForm中加载带有图像的TreeView

时间:2012-06-17 19:19:33

标签: c# xml winforms visual-studio-2010 treeview

在Winform中,我有一个UserControl TreeView,它从XML文件加载实时数据。 XML文件在treeView中成功加载。

我想为不同的数据集生成带有不同图像的TreeView。此链接说明了为特定数据数组生成树视图。 [http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.imagelist] [1]

如何为每个父节点和子节点添加不同的图像,在XML中,我想为Global Files SectionData添加不同的图像。请用一些片段向我解释。

<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>

1 个答案:

答案 0 :(得分:1)

TreeNode类未密封,因此您可以构建一个自定义节点类型。

     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);

要绘制图像,TreeView依赖于ImageView组件。 This link解释了如何以编程方式加载图片