Let us see how to add controls like the CheckBox to a TreeView. Creating a TreeView is quite simple. You can either explicitly define items in the XAML file or bind data programmatically.
Just drag and drop a TreeView Control from the Toolbox and declare a couple of items as shown below:
Now let us say you want to show some checkboxes in the TreeView control. To do so, you have to use the HeaderTemplate property and provide a DataTemplate as shown below:
As you can see, for the first node, we have used the HeaderTemplate property and added a DataTemplate inside it which shows the Checkbox, as shown below:
Note: If you are binding the TreeView programmatically, use a HierarchicalDataTemplate. Let me know if you need an example for that.
Binding Events to the CheckBox inside TreeView
If you observe, the checkbox has a click event attached to it. Let us add some code which will retrieve the Name of the node when the checkbox is clicked. Comments have been added to help you understand the code:
private void cb_Click(object sender, RoutedEventArgs e)
// Get parent item of clicked checkbox
TreeViewItem tvi = GetParentItem((DependencyObject)sender);
if (tvi != null)
// Display the Label of the CheckBox
private static TreeViewItem GetParentItem(DependencyObject obj)
if (obj != null)
// Get the Object Parents Object in the Visual Tree
DependencyObject objParent = VisualTreeHelper.GetParent(obj);
// Cast it to TreeViewItem
TreeViewItem parentItem = objParent as TreeViewItem;
// recursive code
return (parentItem != null) ? parentItem : GetParentItem(objParent);
As soon as the checkbox is clicked, a MessageBox pops up showing the Label of the Node.
Note: If you have multiple checkboxes, create a generic handler and add the click code in it.
Will you give this article a +1 ? Thanks in advance