Posted by: Zeeshan Amjad | December 7, 2009

Using Hierarchical Data Template with VB Code


For hierarchical data type, we can define hierarchical data template. This template can be used with tree control, menu control or any hierarchical data template. This time we are going to use hierarchical data template with VB code. Here is a class diagram to show the hierarchical data template class.

Template_02

Here is a vb code to define hierarchical data template

  1: Dim hdt As HierarchicalDataTemplate
  2: hdt = New HierarchicalDataTemplate(GetType(State))
  3: hdt.ItemsSource = New Binding("Cities")
  4: Dim tb As FrameworkElementFactory
  5: tb = New FrameworkElementFactory(GetType(TextBlock))
  6: tb.SetBinding(TextBlock.TextProperty, New Binding("Name"))
  7: tb.SetValue(TextBlock.ForegroundProperty, Brushes.Yellow)
  8: hdt.VisualTree = tb

Here is a complete vb.net code of this program.

  1: Imports System
  2: Imports System.Windows
  3: Imports System.Windows.Controls
  4: Imports System.Collections.Generic
  5: Imports System.Windows.Data
  6: Imports System.Windows.Media
  7: 
  8: Class MyApplication
  9:     Inherits Application
 10:     Public Sub New()
 11:     End Sub
 12: End Class
 13: Class MyWindow
 14:     Inherits Window
 15: 
 16:     Public Sub New()
 17:         Width = 300
 18:         Height = 300
 19:         Title = "Hierarchical Data Template"
 20: 
 21:         Dim stateList As List(Of State)
 22:         stateList = New List(Of State)
 23: 
 24:         Dim hdt As HierarchicalDataTemplate
 25:         hdt = New HierarchicalDataTemplate(GetType(State))
 26:         hdt.ItemsSource = New Binding("Cities")
 27:         Dim tb As FrameworkElementFactory
 28:         tb = New FrameworkElementFactory(GetType(TextBlock))
 29:         tb.SetBinding(TextBlock.TextProperty, New Binding("Name"))
 30:         tb.SetValue(TextBlock.ForegroundProperty, Brushes.Yellow)
 31:         hdt.VisualTree = tb
 32: 
 33: 
 34:         Dim citylist1 As List(Of City)
 35:         citylist1 = New List(Of City)
 36:         citylist1.Add(New City("Baltimore"))
 37:         citylist1.Add(New City("Frederick"))
 38:         citylist1.Add(New City("Frederick"))
 39:         Dim state1 As State
 40:         state1 = New State()
 41:         state1.Name = "Maryland"
 42:         state1.Cities = citylist1
 43: 
 44:         stateList.Add(state1)
 45: 
 46:         Dim citylist2 As List(Of City)
 47:         citylist2 = New List(Of City)
 48:         citylist2.Add(New City("Los Angeles"))
 49:         citylist2.Add(New City("Sacramento"))
 50:         citylist2.Add(New City("San Franscico"))
 51:         citylist2.Add(New City("San Diegao"))
 52:         Dim state2 As State
 53:         state2 = New State()
 54:         state2.Name = "California"
 55:         state2.Cities = citylist2
 56: 
 57:         stateList.Add(state2)
 58: 
 59:         Dim citylist3 As List(Of City)
 60:         citylist3 = New List(Of City)
 61:         citylist3.Add(New City("Houston"))
 62:         citylist3.Add(New City("Dallas"))
 63:         citylist3.Add(New City("Austin"))
 64:         citylist3.Add(New City("San Antino"))
 65:         Dim state3 As State
 66:         state3 = New State()
 67:         state3.Name = "Taxes"
 68:         state3.Cities = citylist3
 69: 
 70:         stateList.Add(state3)
 71: 
 72:         Dim tvi As TreeViewItem
 73:         tvi = New TreeViewItem()
 74:         tvi.ItemsSource = stateList
 75:         tvi.ItemTemplate = hdt
 76: 
 77:         Dim tree As TreeView
 78:         tree = New TreeView
 79:         tree.Background = Brushes.Blue
 80:         tree.Margin = New Thickness(5)
 81:         tree.Items.Add(tvi)
 82: 
 83:         Content = tree
 84:     End Sub
 85: 
 86: End Class
 87: 
 88: Class City
 89:     Private _name As String
 90: 
 91: 
 92:     Public Sub New(ByVal name As String)
 93:         _name = name
 94:     End Sub
 95: 
 96:     Public Property Name() As String
 97:         Get
 98:             Return _name
 99:         End Get
100:         Set(ByVal value As String)
101:             _name = value
102:         End Set
103:     End Property
104: End Class
105: 
106: Class State
107:     Private _name As String
108:     Private _cities As List(Of City)
109: 
110:     Public Sub New()
111:         _cities = New List(Of City)
112:     End Sub
113: 
114:     Public Property Name() As String
115:         Get
116:             Return _name
117:         End Get
118:         Set(ByVal value As String)
119:             _name = value
120:         End Set
121:     End Property
122: 
123:     Public Property Cities() As List(Of City)
124:         Get
125:             Return _cities
126:         End Get
127:         Set(ByVal value As List(Of City))
128:             _cities = value
129:         End Set
130:     End Property
131: End Class
132: 
133: Public Class Wpf
134:     Public Shared Sub Main()
135:         Dim win As MyWindow
136:         win = New MyWindow()
137: 
138:         Dim app As MyApplication
139:         app = New MyApplication()
140:         app.Run(win)
141:     End Sub
142: End Class
143: 

This is the output of this program.

HierarchicalDataTemplateOutput


Responses

  1. I think your blog and views about hierarchical data template are very important an this helps us in using hierarchical datagrid.Thanks for thought’s.

    • Thanks to visit and like my blog.

      Regards
      Zeeshan Amjad


Leave a comment

Categories