目錄:
介紹
綁定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) 節點:
<?xml version="1.0" encoding="utf-8"?>
<addressbook>
<contacts id="Contacts">
<contact id="Alex">
<email id="popmail">
someone@some_pop_mail.net</email>
<city>Edinburgh</city>
<country>United Kingdom</country>
</contact>
<contact id="Rebekah">
<email id="webmail">
someone@some_web_mail.net</email>
<city>Papakura</city>
<country>New Zealand</country>
</contact>
<contact id="Justin">
<email id="webmail">
someone_else@some_web_mail.com</email>
<city>Muriwai</city>
<country>New Zealand</country>
</contact>
</contacts>
</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的節點時,你將看到圖一的內容: