Skip to main content
Email Password   helpLost your password?

TreeViewAdv in Multi-Column mode

Introduction

Working on several different projects, I was needed to display and edit hierarchical data. Of course, the first thing you will do is to use the standard .NET TreeView control. It works pretty well if you only need basic features. But learning this control to do something more complex is not an easy job. I could not find an alternative TreeView control which is free and fully meets my needs, so finally I decided to write my own.

The architecture of this control comes mainly from the Java Swing component, with some modifications. These are the key features of the TreeViewAdv control:

The following screenshots illustrate the TreeViewAdv features:

Drag&Drop highlighting

Multiselection

Using ComboBox to edit node

Model-View Architecture

I really like the Model-View pattern, and decided to use it in this control. The main idea of this pattern is to split the model (business object) from its visualization (control). If the model changes, it notifies the view by firing corresponding events. The view asks the model for details, if needed, and displays the changes. The model is described by ITreeModelInterface:

public interface ITreeModel
{
    IEnumerable GetChildren(TreePath treePath);
    bool IsLeaf(TreePath treePath);

    event EventHandler<TreeModelEventArgs> NodesChanged; 
    event EventHandler<TreeModelEventArgs> NodesInserted;
    event EventHandler<TreeModelEventArgs> NodesRemoved; 
    event EventHandler<TreePathEventArgs> StructureChanged;
}

It�s very simple, and you need to implement only two methods. GetChildren should return the list of child nodes of the specified parent (empty for root nodes). IsLeaf method tells TreeView whether it should try to read child nodes of the specified parent. If you wish TreeView to dynamically track model changes, you need to use one of several events of the ITreeModel interface. The most common is the StructureChanged event, which cause the TreeView to fully refresh the specified node (or empty, for the whole model). For example, see the default implementation of the ITreeModel interface � the TreeModel class.

To specify the exact node in the model, TreePath class is used. It stores the path from the root to the node, in the FullPath property.

public class TreePath
{
    public object[] FullPath{ get; }
    public object LastNode{ get; }
    public object FirstNode{ get; }
}

Using TreeView

In the source code, you can find two examples of how to use TreeViewAdv. The simplest way is to use TreeModel. All you need is to populate it with data and display it in the view:

_model = new TreeModel();
_model.Nodes.Add(new Node("Root"));
_tree.Model = _model;

The Node class, which is used in TreeModel, contains only the �Text� and �IsChecked� properties. If you need additional properties, you can create an ancestor of the Node class and use it in TreeModel.

But to use the full power of the TreeViewAdv, you should create your own realization of the ITreeModel interface. See the folder browser presented in the source code, for an example.

Customizing TreeView

There are a number of properties which help to customize the look and behavior of the TreeView. The main ones are:

NodeControls

The standard TreeView can display only one icon, CheckBox, and Label for each node. In TreeViewAdv, you can use any number of NodeControl. All controls must inherit from the �NodeControl� abstract class. Inherited classes should contain the code to draw the control and the code to respond on user actions � mouse and keyboard events.

NodeControl

This is the class diagram of all NodeControls provided by the library:

Class diagram

The BindableControl class provides a �DataPropertyName� which is used in the control to read and write data to the node. All that you need is to specify the name of the property of your class.

Terms and Conditions

The TreeViewAdv control is provided as free software with open source code. You can use it in your applications if the original copyright is kept.

The latest version of TreeViewAdv is always available here. Please feel free to add your comments and suggestions in the forum there.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralAssociate a context menu with each NodeControl of a TreeViewAdv : possible ? Pin
michel_page38
11:04 21 Nov '09  
GeneralLatest code no worky Pin
mjanulaitis1234
8:21 19 Nov '09  
GeneralImproved GetRowAt(Point point) Pin
YevgenBorodkin
1:05 18 Nov '09  
AnswerImproved AutoSizeColumn function Pin
ub3rst4r
9:54 8 Nov '09  
GeneralRe: Improved AutoSizeColumn function Pin
Steve Towner
18:15 14 Nov '09  
GeneralRe: Improved AutoSizeColumn function Pin
YevgenBorodkin
0:49 18 Nov '09  
QuestionIs it possible to make tree column frozen? Pin
Marcin Smolka
5:07 29 Oct '09  
GeneralMore Documentation - Cannot get text of node to display Pin
Killme007007
19:41 13 Oct '09  
GeneralPerhaps a content bug in the TreeView control (SMALL) Pin
LimitedAtonement
7:36 5 Oct '09  
GeneralHow to disable the default tooltip of expandable node in asp.net 2.0 Pin
hetak\lkumar
3:03 30 Sep '09  
GeneralPerformance Problem with 10.000 or More Nodes Pin
Michael Biener
3:47 28 Sep '09  
AnswerRe: Performance Problem with 10.000 or More Nodes Pin
yetibrain
1:54 30 Sep '09  
GeneralAdvTreeView Multi Line Header Pin
areB
5:17 19 Sep '09  
GeneralTreeView used in DBSourceTools Pin
NathanRozentals
4:23 17 Sep '09  
QuestionCannot set model for inserted root Node. Pin
YevgenBorodkin
7:19 20 Aug '09  
AnswerRe: Cannot set model for inserted root Node. Pin
dan_bock
6:34 7 Oct '09  
Questioncannot run this project [modified] Pin
coolkid1712
20:05 15 Aug '09  
AnswerRe: cannot run this project Pin
coolkid1712
2:01 16 Aug '09  
GeneralNodes empty Pin
superbloki
3:49 10 Aug '09  
GeneralRe: Nodes empty Pin
dan_bock
7:11 7 Oct '09  
GeneralTreeView with mouse click Pin
Osada Wijekoon
20:59 7 Aug '09  
GeneralThank You!! Pin
iccb1013
23:57 21 Jul '09  
Generalcan use vb.net code Pin
bas2008_57
23:42 20 Jul '09  
QuestionHow to make font of the Node Text to bold? Pin
Vivek_Minhas
20:50 8 Jul '09  
NewsAdvanced TreeView version 1.7.0.0 code available on source forge Pin
Vivek_Minhas
20:24 8 Jul '09  


Last Updated 10 Jul 2006 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009