Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Mon Sep 12, 2005 1:52 am Post subject: How do I add a node to the Tree? |
|
|
Virtual Tree's programming model is a bit different to the standard .NET tree and list view. It is fully data bound - which means that you don't add visual display elements (such as rows or nodes) yourself. Instead you provide it with a data source (such as a list of objects) and it builds the visual display. If you've used data bound ListBox and ComboBox controls you'll know how much simpler this makes the programming task. Virtual Tree brings this same power and simplicity to Tree and List View controls.
So, to return to the original question, you don't create rows directly. Instead you create a list of objects that you want to display (for instance you can create an ArrayList of FileInfo objects) and set this as the data source for your tree. Then you use the Virtual Tree designer (right click on the control) to create RowBindings for the objects you are displaying. The RowBindings tell Virtual Tree how to map the properties of a given object type to the user interface display elements. If you use the Auto Generate function it will automatically create columns for each for the public properties of the given object type and RowBindings that map the object properties to columns.
To add a new row you simply add an object to the collection that you set as the data source. If your data source collection supports two way data binding (via the IBindingList interface) then Virtual Tree will automatically update to display the change. If it is a simple collection, such as an ArrayList, then you can call VirtualTree.UpdateRows to refresh the display after making changes to the collection.
Why do we use this approach? In a typical application using a standard TreeView you have to write a large amount of code handling the relationship between your data model and the display. Virtual Tree's data binding model handles this for you automatically and seemlessly - allowing you to concentrate on the core functionality of your application.
The other main advantage of the fully data bound approach is that it enables Virtual Tree to be very quick and resource efficient - because it creates the display elements it needs to display the current data on the fly. Other tree controls that support some form of databinding just build it on top of their standard unbound mode. This means that they parse the data source and create all the user interface elements (nodes etc) upfront when the data source is set. For a very large data source this can take a long time and consume a great deal of memory. You can only leverage the true power and performance of data binding by abandoning the traditional unbound model altogether. _________________ Infralution Support |
|