2012-06-17 24 views
1

在Winform中,我有一個UserControl TreeView,它從XML文件加載實時數據。 XML文件在treeView中成功加載。C#:在UserControl中加載帶有圖像的TreeView WinForm

我想爲不同的數據集生成不同圖像的TreeView。此鏈接解釋爲特定的數據陣列生成樹視圖。 [http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.imagelist][1]

如何在XML中爲每個父節點和子節點添加不同的圖像,我想爲GlobalFilesSectionData添加不同的圖像。請用一些片段向我解釋。

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

你用什麼來解析XML? XmlReader,XmlDocument,XPathNavigator,XDocument? – alexm

+0

@ alexm:我是uisng'XmlDocument xDoc = new XmlDocument();' – linguini

回答

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解釋如何以編程方式加載圖像

+0

我怎樣才能爲樹選擇添加圖片? – linguini

+0

@linguini你需要預先將它們加載到ImageList中。 – alexm

+0

@linguini你從哪裏獲得圖像?它們在編譯時是否可用? – alexm