Posted by: Zeeshan Amjad | December 15, 2009

Using Check box in Tree Control


Using check box is very similar to using any other control inside the data template and hierarchical data template. Here is simple XAML code to insert check box in tree control.

  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="400" Width="400">
  6:     <Grid>
  7:         <TreeView Name="tree" Margin="5" Background="AliceBlue" ItemsSource="{x:Static local:Window1.stateList}">                        
  8:             <TreeView.ItemTemplate>
  9:                 <HierarchicalDataTemplate ItemsSource="{Binding Path=Cities}">
 10:                     <StackPanel Margin="1" Orientation="Horizontal">
 11:                         <CheckBox Margin="1" IsChecked="{Binding IsVisited}"/>
 12:                         <TextBlock Margin="1" Text="{Binding Name}"/>
 13:                     </StackPanel>
 14:                     
 15:                     <HierarchicalDataTemplate.ItemTemplate>
 16:                         <DataTemplate>
 17:                             <StackPanel Margin="1" Orientation="Horizontal">
 18:                                 <CheckBox Margin="1" IsChecked="{Binding IsVisited}"/>
 19:                                 <TextBlock Margin="1" Text="{Binding Name}"/>
 20:                             </StackPanel>
 21:                         </DataTemplate>
 22:                     </HierarchicalDataTemplate.ItemTemplate>
 23:                 </HierarchicalDataTemplate>
 24:             </TreeView.ItemTemplate>
 25:         </TreeView>
 26:     </Grid>
 27: </Window>
 28: 

Here is complete C# code of this 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:         static public List<State> stateList = new List<State>();
 23: 
 24:         public Window1()
 25:         {
 26:             InitializeComponent();            
 27: 
 28:             List<City> citylist1 = new List<City>();
 29:             citylist1.Add(new City("Baltimore", true));
 30:             citylist1.Add(new City("Frederick", true));
 31:             citylist1.Add(new City("Rockville", true));
 32:             State state1 = new State();
 33:             state1.Name = "Maryland";
 34:             state1.IsVisited = true;
 35:             state1.Cities = citylist1;
 36: 
 37:             stateList.Add(state1);
 38: 
 39:             List<City> citylist2 = new List<City>();
 40:             citylist2.Add(new City("Los Angeles", true));
 41:             citylist2.Add(new City("Sacramento", false));
 42:             citylist2.Add(new City("San Francisco", true));
 43:             citylist2.Add(new City("San Diego", false));
 44:             State state2 = new State();
 45:             state2.Name = "California";
 46:             state2.IsVisited = true;
 47:             state2.Cities = citylist2;
 48: 
 49:             stateList.Add(state2);
 50: 
 51:             List<City> citylist3 = new List<City>();
 52:             citylist3.Add(new City("Houston", true));
 53:             citylist3.Add(new City("Dallas", false));
 54:             citylist3.Add(new City("Austin", false));
 55:             citylist3.Add(new City("San Antonio", false));
 56:             State state3 = new State();
 57:             state3.Name = "Taxes";
 58:             state3.IsVisited = true;
 59:             state3.Cities = citylist3;
 60: 
 61:             stateList.Add(state3);
 62:         }
 63:     }
 64: 
 65:     public class City
 66:     {
 67:         public City(String name, bool isVisited)
 68:         {
 69:             Name = name;
 70:             IsVisited = isVisited;
 71:         }
 72: 
 73:         public String Name
 74:         { get; set; }
 75: 
 76:         public bool IsVisited
 77:         { get; set; }
 78:     }
 79: 
 80:     public class State
 81:     {
 82:         public State()
 83:         {
 84:             this.Cities = new List<City>();
 85:         }
 86: 
 87:         public String Name
 88:         { get; set; }
 89: 
 90:         public bool IsVisited
 91:         { get; set; }
 92: 
 93:         public List<City> Cities
 94:         { get; set; }
 95:     }
 96: }
 97: 

Here is the output of this program.

HierarchicalDataTemplateOutput_05

Advertisements

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

%d bloggers like this: