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 C# code. Here is a class diagram to show the hierarchical data template class.
Here out XAML is very simple. We are only defining tree control in XAML and does rest of the template in code.
1: <Window x:Class="WpfTreeView.Window1"2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"4: xmlns:local="clr-namespace:WpfTreeView"5: Title="Hierarchical Data Template" Height="300" Width="300">6: <Grid>7: <TreeView Name="tree" Margin="5">8: <TreeView.Background>9: <LinearGradientBrush>10: <GradientStop Offset="0" Color="AliceBlue"/>11: <GradientStop Offset="0.5" Color="Blue"/>12: <GradientStop Offset="1" Color="AliceBlue"/>13: </LinearGradientBrush>14: </TreeView.Background>15: </TreeView>16: </Grid>17: </Window>18:Here is a code to define hierarchical data template in code.
1: HierarchicalDataTemplate hdt =2: new HierarchicalDataTemplate(typeof(State));3: hdt.ItemsSource = new Binding("Cities");4: FrameworkElementFactory tb =5: new FrameworkElementFactory(typeof(TextBlock));6: tb.SetBinding(TextBlock.TextProperty, new Binding("Name"));7: tb.SetValue(TextBlock.ForegroundProperty, Brushes.Yellow);8: hdt.VisualTree = tb;9:This template will define the hierarchy that cities are part of state. In addition we also define the background color of tree view items in this code. Here is a complete C# code of the program.
1: using System;2: using System.Collections.Generic;3: using System.Linq;4: using System.Text;5: using System.Windows;6: using System.Windows.Controls;7: using System.Windows.Data;8: using System.Windows.Documents;9: using System.Windows.Input;10: using System.Windows.Media;11: using System.Windows.Media.Imaging;12: using System.Windows.Navigation;13: using System.Windows.Shapes;14:15: namespace WpfTreeView16: {17: /// <summary>18: /// Interaction logic for Window1.xaml19: /// </summary>20: public partial class Window1 : Window21: {22: public Window1()23: {24: InitializeComponent();25:26: List<State> stateList = new List<State>();27:28: HierarchicalDataTemplate hdt =29: new HierarchicalDataTemplate(typeof(State));30: hdt.ItemsSource = new Binding("Cities");31: FrameworkElementFactory tb =32: new FrameworkElementFactory(typeof(TextBlock));33: tb.SetBinding(TextBlock.TextProperty, new Binding("Name"));34: tb.SetValue(TextBlock.ForegroundProperty, Brushes.Yellow);35: hdt.VisualTree = tb;36:37: List<City> citylist1 = new List<City>();38: citylist1.Add(new City("Baltimore"));39: citylist1.Add(new City("Frederick"));40: citylist1.Add(new City("Rockville"));41: State state1 = new State();42: state1.Name = "Maryland";43: state1.Cities = citylist1;44:45: stateList.Add(state1);46:47: List<City> citylist2 = new List<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: State state2 = new State();53: state2.Name = "California";54: state2.Cities = citylist2;55:56: stateList.Add(state2);57:58: List<City> citylist3 = new List<City>();59: citylist3.Add(new City("Houston"));60: citylist3.Add(new City("Dallas"));61: citylist3.Add(new City("Austin"));62: citylist3.Add(new City("San Antino"));63: State state3 = new State();64: state3.Name = "Taxes";65: state3.Cities = citylist3;66:67: stateList.Add(state3);68:69: TreeViewItem tvi = new TreeViewItem();70: tvi.ItemsSource = stateList;71: tvi.ItemTemplate = hdt;72:73: tree.Items.Add(tvi);74: }75: }76:77: public class City78: {79: public City(String name)80: {81: Name = name;82: }83:84: public String Name85: { set; get; }86: }87:88: public class State89: {90: public State()91: {92: this.Cities = new List<City>();93: }94:95: public String Name96: { get; set; }97:98: public List<City> Cities99: { get; set; }100: }101: }102:Here is the output of this program.


Dude i asked the same question on stackoverflow here:
http://stackoverflow.com/questions/4428159/how-to-make-groups-or-hierarchies-in-treeview-of-c-wpf
i am going to take the liberty of putting link of this aritcle there, great work man, God Bless you
By: hassan on December 16, 2010
at 3:33 am
[...] arrayTreeView.ItemsSource = ParentTreeViewNodeList; this is the link where i took help from: http://zamjad.wordpress.com/2009/12/06/using-hierarchical-data-template-with-c-code/#comment-446thanks [...]
By: Setting font of parent in nodes in WPF Tree View to Bold using code behind - Question Lounge on December 20, 2010
at 2:34 pm