當前位置:開發者網絡 >> 技術教程 >> .NET教程 >> XML應用 >> 內容
精彩推薦
分類最新教程
分類熱點教程
    
創建可編輯的xml文檔(之五)執行中的treeview 控件
作者:未知
日期:2004-07-03
人氣:
投稿:xiaxia(轉貼)
來源:未知
字體:
收藏:加入瀏覽器收藏
以下正文:
執行中的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文檔。因此當你關閉應用程序時,代碼保存所有的修改到文檔中。希望這些能夠對你的項目有一些好的啟發,或者豐富你的組件或者接口的開發。
相關文章: