Posted by: Zeeshan Amjad | December 6, 2009

Using Hierarchical Data Template with C# 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 C# code. Here is a class diagram to show the hierarchical data template class.

 

Template_02

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 WpfTreeView
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {
 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 City
 78:     {
 79:         public City(String name)
 80:         {
 81:             Name = name;
 82:         }
 83: 
 84:         public String Name
 85:         { set; get; }
 86:     }
 87: 
 88:     public class State
 89:     {
 90:         public State()
 91:         {
 92:             this.Cities = new List<City>();
 93:         }
 94: 
 95:         public String Name
 96:         { get; set; }
 97: 
 98:         public List<City> Cities
 99:         { get; set; }
100:     }
101: }
102: 

Here is the output of this program.

HierarchicalDataTemplate

About these ads

Responses

  1. 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

  2. [...] 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 [...]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.

Join 510 other followers

%d bloggers like this: