Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Create Editable XML Documents Using XPath and the TreeView Control : Page 6

Adding drag and drop support to your TreeView control needn't be taxing. With XML and Xpath, you can get most of that support for free.


advertisement
The TreeView Control in Action
That's pretty much it. The two versions of Listing 4 (VB.NET version, or C# version) contain the final code for the hybrid TreeView control. I've restructured and tidied the code a bit to make it easier to use, and added a KeyDown handler to support some of the default accelerators, Control-N (new), F2 (edit), and DEL (delete).

There didn't seem any need to raise additional custom events; accordingly, the final API is a single method and eight attributes, which you'll find listed in Table 1. The majority of these are simply flags, and switched on by default; I added them to let you fine-tune your choice of the editing features your application will support.

Table 1. XmlTreeView Control Attributes and Methods: All you need to add drag-and-drop support to your TreeView applications are these eight attributes and one method.

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.



Load the sample project that accompanies this article to see the hybrid TreeView control in action. The sample project is a cut-down version of the catalog administration utility I mentioned earlier. It's worth mentioning is that it takes only four lines of code to implement full drag-drop support, and a further three to let users create new folders.

[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 shows a screenshot of the catalog administrator form, and Figure 3 shows a detail of the final catalog.



Alex Hildyard  is a freelance software consultant and writer, specializing in Web technology. He can be contacted .
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Thanks for your registration, follow us on our social networks to keep up-to-date