
Introduction
Binding different controls with similar data types can effectively decrease the time required for coding. This can also help different programmers to face similar problems during their coding and they instantly find the solution to their questions in result. Although the data binding methods and approaches are developed in different frameworks but there still remain some unexpected demands for data bindings such as what we discuss here in the case of ListViews and TreeViews.
Background
Basically TreeViews are widely used wherever user does not have a comprehensive map of the data structure inside the code in mind or when he/she decides to jump over nodes (e.g. drives and folders) by only one click. On the other hand ListViews – in form of what we have in Explorer - are suitable for users who just got used to the data structure. This supports this claim that TreeViews are suitable for data management and exploration but ListViews are better for applying data in program conventions. The first one acts in X/Y direction but the last one acts through Z.
Although the SubItems property of ListViews can improve the extensibility of ListViews, this property is not inherited from ListViewItem so it can not be converted to an identical data type to develop a tree shaped flow of data.
The key to connect these two controls is to save the current TreeNode in the ListView control.
Using the Code
The code is extremely simple. First of all we have to declare a ListView inherited class where we define our custom ListView.
Public Class BindedListView
Inherits ListView
End Class
Now a TreeView object should be declared in the ListView inherited class
Private _TreeView As TreeView
Public Property TreeView() As TreeView
Get
Return _TreeView
End Get
Set(ByVal value As TreeView)
If value IsNot Nothing Then
_TreeView = value
Initialize()
End If
End Set
End Property
This TreeView is the main reference for our ListView treatments. Obviously, the nodes in the TreeView can not be replaced by TreeView because they have different types so another object should be declared. The _treenode object is the first node in the TreeView that represents the root node of the TreeView.
Private _treenode As TreeNode
The initialize subroutine sets initial values as items.
Private Sub Initialize()
Items.Clear()
Items.Add("...")
_treenode = _TreeView.Nodes(0)
SmallImageList = _TreeView.ImageList
LargeImageList = _TreeView.ImageList
For i As Integer = 0 To _treenode.Nodes.Count - 1
Items.Add(_treenode.Nodes(i).Text)
Next
End Sub
Here the OnMouseClick event of the ListView has been overridden to determine whether user has clicked a TreeNode item or the first item which changes the current path to the upper path.
Protected Overrides Sub OnMouseDoubleClick(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDoubleClick(e)
If SelectedIndices.Count = 1 Then
If SelectedIndices(0) > 0 Then
GoIn()
Else
GoOut()
End If
End If
End Sub
In GoIn and GoOut subroutines the new TreeNode is being detected and its ChildNodes are being casted on the ListView. Finally, the old _treenode is replaced by new _treenode and a PathChanged event rises.
Sub GoIn()
Try
Dim inner As Integer = SelectedIndices(0) - 1
If _treenode.Nodes(inner).Nodes.Count = 0 Then Exit Sub
Items.Clear()
Items.Add("...")
For i As Integer = 0 To _treenode.Nodes(inner).Nodes.Count - 1
Items.Add(_treenode.Nodes(inner).Nodes(i).Text, _treenode.Nodes(inner).Nodes(i).ImageIndex)
Next
_treenode = _treenode.Nodes(inner)
Catch ex As Exception
End Try
RaiseEvent PathChanged(_treenode.FullPath)
End Sub
Sub GoOut()
Try
If _treenode.Parent.Nodes.Count = 0 Then Exit Sub
Items.Clear()
Items.Add("...")
For i As Integer = 0 To _treenode.Parent.Nodes.Count - 1
Items.Add(_treenode.Parent.Nodes(i).Text, _treenode.Parent.Nodes(i).ImageIndex)
Next
_treenode = _treenode.Parent
Text = _treenode.FullPath
Catch ex As Exception
End Try
RaiseEvent PathChanged(_treenode.FullPath)
End Sub