目录:
介绍
绑定 xml 文档到 treeview 控件
过滤 xml 数据
执行拖放操作
执行删除,改名,插入操作
使用中的 treeview 控件
通过 xml 和 xpath 你可以毫不费力的为你的 treeview 控件增加拖放甚至更多的功能- by Alex Hildyard
最近,我一直在开发一个用来维护在线目录的用户界面工具,因为这个目录包含太多的产品,所以用一些方法对他们分类是很有意义的。目录管理员将需要有删除和定义新的目录的能力,目录和目录之间进行嵌套的能力,还要用巧妙的方式让目录和产品看上去很直观。
象这样的分类情节迫切需要一个按照种类分类的分等级视图,第一:在数据和它的表示之间的映射通常很微不足道的( trivial ),因为 treeview 控件的对象模型是自身分等级的。第二:展开一个独立的树节点的能力将用多重级别浏览数据变得更容易 . 最后 : 在 TreeView 中拖放文件夹是快速处理复杂层次非常简单和吸人注意的方法。
几分钟后,我意识到我脑子中的这个应用程序就是 Windows Explorer ( windows 资源管理器) , 并且我要重写它,用产品目录代替文件夹,用产品项目代替文件,甚至我可以快速的实现类似创建或者删除文件夹,执行拖放等操作。如果我以后为一个关系数据编写接口,或者编写一个联系管理程序,或者开发一个追踪我的家族族谱的工具,那么我将会发现我做的都是相同的事情。
这么做是很没有意义的,我需要找到一个为 treeview 控件提供分级数据源的通用方法,这个就好象为一个数据表格控件( data grid )在数据库创建一个数据表( database table )一样,并且要能够很方便的实现创建、删除、改名、移动、和拖放数据元素的功能,而不需要顾忌询问中的数据源内容的结构
为 treeview 控件创建 xml 文档:
根据 treeview 的层次机构, xml 是非常合乎逻辑的数据格式,你可以用少于 6 行的代码实现在 treeview 控件中显示 xml 文档,假设你有一个类似下面这样的一个 xml 文档 ,它包含很多联系( contact ) 节点:
1<addressbook>
2
3
4 <contacts id="Contacts">
5
6
7 <contact id="Alex">
8
9
10 <email id="popmail">
11
12
13 someone@some_pop_mail.net</email>
14
15
16 <city>Edinburgh</city>
17
18
19 <country>United Kingdom</country>
20
21
22 </contact>
23
24
25 <contact id="Rebekah">
26
27
28 <email id="webmail">
29
30
31 someone@some_web_mail.net</email>
32
33
34 <city>Papakura</city>
35
36
37 <country>New Zealand</country>
38
39
40 </contact>
41
42
43 <contact id="Justin">
44
45
46 <email id="webmail">
47
48
49 someone_else@some_web_mail.com</email>
50
51
52 <city>Muriwai</city>
53
54
55 <country>New Zealand</country>
56
57
58 </contact>
59
60
61 </contacts>
62
63
64 </addressbook>
你可以很容易的通过递归调用将所有的数据元素组装到 treeview 控件中,把所有的 XML 文档节点添加到 treeview 中以后,就可以通过维护 treeview 控件来维护维护 xml 文档的节点关系
[C#]
private void populateTreeControl(
System.Xml.XmlNode document,
System.Windows.Forms.TreeNodeCollection nodes)
{
foreach (System.Xml.XmlNode node in
document.ChildNodes)
{
// If the element has a value, display it;
// otherwise display the first attribute
// (if there is one) or the element name
// (if there isn't)
string text = (node.Value != null ? node.Value :
(node.Attributes != null &&
node.Attributes.Count > 0) ?
node.Attributes[0].Value : node.Name);
TreeNode new_child = new TreeNode(text);
nodes.Add(new_child);
populateTreeControl(node, new_child.Nodes);
}
}
[VB]
Private Sub populateTreeControl( _
ByVal document As System.Xml.XmlNode, _
ByVal nodes As _
System.Windows.Forms.TreeNodeCollection)
Dim node As System.Xml.XmlNode
For Each node In document.ChildNodes
' If the element has a value, display it;
' otherwise display the first attribute
' (if there is one) or the element name
' (if there isn't)
Dim [text] As String
If node.Value <> Nothing Then
[text] = node.Value
Else
If Not node.Attributes Is Nothing And _
node.Attributes.Count > 0 Then
[text] = node.Attributes(0).Value
Else
[text] = node.Name
End If
End If
Dim new_child As New TreeNode([text])
nodes.Add(new_child)
populateTreeControl(node, new_child.Nodes)
Next node
End Sub
现在,你可以新建一个 windows 窗体,拖放一个 treeview 控件到窗体上,添加下面三行到你的数据文件中:
[C#]
System.Xml.XmlDocument document =
new System.Xml.XmlDataDocument();
document.Load("../../contacts.xml");
populateTreeControl(document.DocumentElement,
treeView1.Nodes);
[VB]
Dim document As New System.Xml.XmlDataDocument()
document.Load("../contacts.xml")
populateTreeControl(document.DocumentElement, _
TreeView1.Nodes)
当你展开 treeview 的节点时,你将看到图一的内容: