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.
Here is a vb code to define hierarchical data template
1: Dim hdt As HierarchicalDataTemplate2: hdt = New HierarchicalDataTemplate(GetType(State))3: hdt.ItemsSource = New Binding("Cities")4: Dim tb As FrameworkElementFactory5: tb = New FrameworkElementFactory(GetType(TextBlock))6: tb.SetBinding(TextBlock.TextProperty, New Binding("Name"))7: tb.SetValue(TextBlock.ForegroundProperty, Brushes.Yellow)8: hdt.VisualTree = tbHere 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 Sub12: End Class13: Class MyWindow
14: Inherits Window
15:16: Public Sub New()17: Width = 30018: Height = 30019: Title = "Hierarchical Data Template"
20:21: Dim stateList As List(Of State)22: stateList = New List(Of State)23:24: Dim hdt As HierarchicalDataTemplate25: hdt = New HierarchicalDataTemplate(GetType(State))26: hdt.ItemsSource = New Binding("Cities")27: Dim tb As FrameworkElementFactory28: tb = New FrameworkElementFactory(GetType(TextBlock))29: tb.SetBinding(TextBlock.TextProperty, New Binding("Name"))30: tb.SetValue(TextBlock.ForegroundProperty, Brushes.Yellow)31: hdt.VisualTree = tb32: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 State40: state1 = New State()
41: state1.Name = "Maryland"
42: state1.Cities = citylist143: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 State53: state2 = New State()
54: state2.Name = "California"
55: state2.Cities = citylist256: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 State66: state3 = New State()
67: state3.Name = "Taxes"
68: state3.Cities = citylist369:70: stateList.Add(state3)71:72: Dim tvi As TreeViewItem73: tvi = New TreeViewItem()
74: tvi.ItemsSource = stateList75: tvi.ItemTemplate = hdt76:77: Dim tree As TreeView78: tree = New TreeView
79: tree.Background = Brushes.Blue80: tree.Margin = New Thickness(5)
81: tree.Items.Add(tvi)82:83: Content = tree84: End Sub85:86: End Class87:88: Class City
89: Private _name As String90:91:92: Public Sub New(ByVal name As String)93: _name = name94: End Sub95:96: Public Property Name() As String97: Get
98: Return _name
99: End Get100: Set(ByVal value As String)101: _name = value102: End Set103: End Property104: End Class105:106: Class State
107: Private _name As String108: Private _cities As List(Of City)109:110: Public Sub New()111: _cities = New List(Of City)112: End Sub113:114: Public Property Name() As String115: Get
116: Return _name
117: End Get118: Set(ByVal value As String)119: _name = value120: End Set121: End Property122:123: Public Property Cities() As List(Of City)124: Get
125: Return _cities
126: End Get127: Set(ByVal value As List(Of City))128: _cities = value129: End Set130: End Property131: End Class132:133: Public Class Wpf134: Public Shared Sub Main()135: Dim win As MyWindow136: win = New MyWindow()
137:138: Dim app As MyApplication139: app = New MyApplication()
140: app.Run(win)141: End Sub142: End Class143:This is the output of this program.
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.
By: net grid on April 19, 2012
at 12:40 am
Thanks to visit and like my blog.
Regards
Zeeshan Amjad
By: Zeeshan Amjad on April 19, 2012
at 9:32 am