【问题标题】:C#: Load TreeView with Images in UserControl WinFormC#:在 UserControl WinForm 中加载带有图像的 TreeView
【发布时间】:2012-06-17 19:19:33
【问题描述】:

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

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

如何为每个父节点和子节点添加不同的图像,在 XML 中,我想为 GlobalFilesSectionData 添加不同的图像。请用一些sn-p向我解释一下。

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

【问题讨论】:

  • 你用什么来解析xml? XmlReader、XmlDocument、XPathNavigator、XDocument?
  • @alexm: 我是XmlDocument xDoc = new XmlDocument();

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


【解决方案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 解释如何以编程方式加载图像

【讨论】:

  • 如何为树选择添加图像?
  • @linguini 您需要将它们预加载到 ImageList 中。
  • @linguini 你从哪里得到图像——它们在编译时可用吗?
  • 只是为了验证,我从本地驱动器加载图像。我应该将它添加到 imageList1 吗???
  • 如果您可以在设计时将它们添加到图像列表中,它将简化其余代码。我实际上假设这是您已经在做的事情..
猜你喜欢
  • 2014-01-18
  • 1970-01-01
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多