簡體   English   中英

C#:使用UserControl WinForm中的圖像加載TreeView

[英]C#: Load TreeView with Images in UserControl WinForm

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

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組件。 此鏈接說明了如何以編程方式加載圖像

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM