C#:在UserControl WinForm中加载带有图像的TreeView
在Winform中,我有一个UserControl树视图,它从XML文件加载实时数据。XML文件已成功加载到treeView中 我想为不同的数据集生成具有不同图像的TreeView。此链接说明如何为特定的数据数组生成treeview。[http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.imagelist][1] 如何为每个父节点和子节点添加不同的图像,在XML中,我想为C#:在UserControl WinForm中加载带有图像的TreeView,c#,xml,winforms,visual-studio-2010,treeview,C#,Xml,Winforms,Visual Studio 2010,Treeview,在Winform中,我有一个UserControl树视图,它从XML文件加载实时数据。XML文件已成功加载到treeView中 我想为不同的数据集生成具有不同图像的TreeView。此链接说明如何为特定的数据数组生成treeview。[http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.imagelist][1] 如何为每个父节点和子节点添加不同的图像,在XML中,我想为全局文件节和数据添加不同的图像。请给我
全局文件
节
和数据
添加不同的图像。请给我解释一下
<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.01
食品
0
0
1000
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);
抽象类CustomTreeDataNode:TreeNode
{
公共CustomTreeDataNode()
{
}
受保护的void ReadChildNodes(XmlNode父节点,字符串childNodeName)
其中T:CustomTreeDataNode,new()
{
foreach(父节点中的XmlNode节点。SelectNodes(childNodeName))
{
T项=新的T();
项目填写(节点);
节点。添加(项);
}
}
公共空白填充(XmlNode)
{
Nodes.Clear();
初始化属性(节点);
}
受保护的抽象void InitProperties(XmlNode节点);
}
类根节点:CustomTreeDataNode
{
受保护的重写void InitProperties(XmlNode源)
{
Text=“Root”;
ItemIndex=ROOT\u ItemIndex;
SelectedIndex=根目录\u SelectedIndex;
ReadChildNodes(源,“文件”);
}
}
类文件节点:CustomTreeDataNode
{
受保护的重写void InitProperties(XmlNode源)
{
Text=源[“名称”];
ItemIndex=文件\u ItemIndex;
SelectedIndex=文件\u SelectedIndex;
ReadChildNodes(来源,“节”);
}
}
类SectionNode:CustomTreeDataNode
{
受保护的重写void InitProperties(XmlNode源)
{
Text=源[“名称”];
ItemIndex=节_ItemIndex;
SelectedIndex=节\u SelectedIndex;
ReadChildNodes(源,“数据”);
}
}
类数据节点:CustomTreeDataNode
{
受保护的重写void InitProperties(XmlNode源)
{
Text=source.Text;
ItemIndex=数据\项目索引;
SelectedIndex=数据\u SelectedIndex;
}
}
...
RootNode root=新的RootNode();
root.Fill(rootXmlNode);
treeView1.Nodes.Add(根);
要绘制图像,TreeView依赖于ImageView组件。说明如何以编程方式加载图像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);
抽象类CustomTreeDataNode:TreeNode
{
公共CustomTreeDataNode()
{
}
受保护的void ReadChildNodes(XmlNode父节点,字符串childNodeName)
其中T:CustomTreeDataNode,new()
{
foreach(父节点中的XmlNode节点。SelectNodes(childNodeName))
{
T项=新的T();
项目填写(节点);
节点。添加(项);
}
}
公共空白填充(XmlNode)
{
Nodes.Clear();
初始化属性(节点);
}
受保护的抽象void InitProperties(XmlNode节点);
}
类根节点:CustomTreeDataNode
{
受保护的重写void InitProperties(XmlNode源)
{
Text=“Root”;
ItemIndex=ROOT\u ItemIndex;
SelectedIndex=根目录\u SelectedIndex;
ReadChildNodes(源,“文件”);
}
}
类文件节点:CustomTreeDataNode
{
受保护的重写void InitProperties(XmlNode源)
{
Text=源[“名称”];
ItemIndex=文件\u ItemIndex;
SelectedIndex=文件\u SelectedIndex;
ReadChildNodes(来源,“节”);
}
}
类SectionNode:CustomTreeDataNode
{
受保护的重写void InitProperties(XmlNode源)
{
Text=源[“名称”];
ItemIndex=节_ItemIndex;
SelectedIndex=节\u SelectedIndex;
ReadChildNodes(源,“数据”);
}
}
类数据节点:CustomTreeDataNode
{
受保护的重写void InitProperties(XmlNode源)
{
Text=source.Text;
ItemIndex=数据\项目索引;
SelectedIndex=数据\u SelectedIndex;
}
}
...
RootNode root=新的RootNode();
root.Fill(rootXmlNode);
treeView1.Nodes.Add(根);
要绘制图像,TreeView依赖于ImageView组件。解释如何以编程方式加载图像您使用什么来解析xml?XmlReader、XmlDocument、XPathNavigator、XDocument?@alexm:我正在使用XmlDocument xDoc=newXMLDocument()代码>您使用什么来解析xml?XmlReader、XmlDocument、XPathNavigator、XDocument?@alexm:我正在使用XmlDocument xDoc=newXMLDocument()代码>如何为树选择添加图像?@linguini您需要将它们预加载到ImageList中。@linguini从何处获取图像-它们在编译时可用吗?为了验证,我从本地驱动器加载图像。我是否应该将其添加到imageList1???如果您可以在设计时将其添加到ImageList中,这将简化其余的代码。事实上,我假设这是你已经在做的事情..我怎样才能添加图像f