執行中的treeview 控制項
為了更完美,列表4包含了VB.NET version, or C# version 兩個版本的最終treevie 控制項。為了更容易使用,我重新定義了結構和代碼。同時增加了KeyDown 控制來支援一些預設的快速鍵例如:Control-N (建立), F2 (編輯), 和DEL (刪除).
這裡好象不必附加任何事件,因此最終的api 包含一個方法和八個屬性,他們在表1中列出來了,他們大多數都是簡單的標誌,或者預設的開關,我增加他們時為了讓你可以選擇使用來增強你的應用程式的可編輯特徵
表1:xmltreeview 控制項的屬性和方法,你可以將它們用到你的應用程式中
Attribute
Type
Parameter
Description
XmlDropTarget
Get
System.Xml.XmlNode
The XML node corresponding to the currently selected node in the TreeView
ActiveXmlDocument
Get
System.Xml.XmlDocument
The XML document bound to the TreeView. This updates as the tree changes
XPathFilter
Get; Set
string
The XPath filter used to identify the element or attribute whose value will be used to display the folder's name. A folder constitutes the tree view's smallest navigable unit
XmlInsertionNode
Get; Set
System.Xml.XmlNode
The template for a new folder. The TreeView caches this, and clones it when a new folder is required.
DragDropActive
Get; Set
bool
Flag denoting whether a drag/drop operation is currently in progress.
EnableEditNode
Get; Set
bool
Flag denoting whether label editing is supported (default is yes)
EnableDeleteNode
Get; Set
bool
Flag denoting whether folder deletion is supported (default is yes)
EnableInsertNode
Get; Set
bool
Flag denoting whether folder insertion is supported (default is yes)
Method:
Returns
Parameter
Description
Load
void
System.Xml.XmlDocument
Loads the specified XML document and uses it to populate the TreeView. Set XPathFilter prior to calling Load() to define an appropriate view on the underlying data.
調用本文章附帶的sample project 工程,你可以看到執行中的treeview 控制項,這個簡單工程是我前面提到的目錄管理員中一個剪下版本,還有必要說一下,只需要四行就可以實現一個拖放操作,圖三顯示讓使用者建立新的檔案夾集合
[C#]
// Load the XML document from a file
xmlDocument = new System.Xml.XmlDataDocument();
xmlDocument.Load(strXmlDocument);
// After setting the path filter, load the
// document into the TreeView
xmlTreeView1.XPathFilter = "attribute::id";
xmlTreeView1.Load(xmlDocument);
// Defining XmlInsertionNode allows creation of new
// nodes within the TreeView
System.Xml.XmlDocument insert_fragment = new
System.Xml.XmlDocument();
insert_fragment.LoadXml("<product id='New
Item'><description/><ordercode/><price/>
<image/></product>");
xmlTreeView1.XmlInsertionNode =
insert_fragment.DocumentElement;
[VB]
' Load the XML document from a file
xmlDocument = New System.Xml.XmlDataDocument()
xmlDocument.Load(strXmlDocument)
' After setting the path filter, load the
' document into the TreeView
xmlTreeView1.XPathFilter = "attribute::id"
xmlTreeView1.Load(xmlDocument)
' Defining XmlInsertionNode allows creation of new
' nodes within the TreeView
Dim insert_fragment As New System.Xml.XmlDocument()
insert_fragment.LoadXml("<product id='New " & _
"Item'><description/><ordercode/><price/>" & _
"<image/></product>")
xmlTreeView1.XmlInsertionNode =
insert_fragment.DocumentElement
圖二 顯示了一個目錄管理員的表單, 圖三顯示了一個詳細的最終目錄
Figure 2. The Catalog Administrator Form: This figure shows the hierarchical TreeView and the details for an item in the catalog administrator form.
Figure 3. Final Catalog. The figure shows a detail view of the final catalog.
代碼的其他部分處理產品列表的可編輯性,還有更新xml文檔。因此當你關閉應用程式時,代碼儲存所有的修改到文檔中。希望這些能夠對你的項目有一些好的啟發,或者豐富你的組件或者介面的開發。