1

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

我想为不同的数据集生成具有不同图像的 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>
4

1 回答 1

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 组件。此链接说明如何以编程方式加载图像

于 2012-06-17T20:34:53.587 回答