Click here to Skip to main content
13,458,880 members
Rate this:
Please Sign up or sign in to vote.

I am working on a customised control which is, simply put, a TreeView with ChecBoxes. The TreeView resides inside a ComboBox.
The TreeView being inside a combobox is just a part of the requirement and I have already achieved the relation I wanted between them. My problem starts with binding TreeViewItems with two different bindings. I have reached the current state after a lot of reading and googling. And am really stuck, hence the question here.

The data to back the TreeView is a XML. This XML is loaded in the ComboBox as a string DependencyProperty named as 'Value' and the TreeView binds to that DependencyProperty of the ComboBox. All this is being done in the UI layer because I believe that this is part of the behavior of the custom control that I am creating and hence no view model backing the view. I am willing to stand corrected here.

Here is my XAML
<ComboBox x:Class="HierarchicalCheckComboBox.HCCB"






             d:DesignHeight="30" d:DesignWidth="300" 

        <HierarchicalDataTemplate x:Key="NodeTemplate" 

                                  ItemsSource="{Binding XPath=child::node()}">
            <StackPanel Orientation="Horizontal">
                <CheckBox x:Name="checkBox" IsChecked="{Binding XPath=@checked}" />
                <TextBlock x:Name="text" Text="{Binding XPath=@text}" />
        <XmlDataProvider x:Key="xmlDataProvider" />
    <ComboBoxItem MaxHeight="100" MinHeight="25" >
        <TreeView Name="treeView1" 

                  ItemsSource="{Binding Source={StaticResource xmlDataProvider}, XPath=/root/child}" 

                  ItemTemplate="{StaticResource NodeTemplate}">
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="True" />

This is the partial code behind class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.IO;

namespace HierarchicalCheckComboBox
    /// <summary>
    /// Interaction logic for HCCB.xaml
    /// </summary>
    public partial class HCCB : ComboBox
        bool _settingState = false;
        /// <summary>
        /// The dependency property which will be used to bind with the CLR property 'Value'. This value is set either ways.
        /// </summary>
        public static readonly DependencyProperty ValueProperty;

        static HCCB ()
            ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(HCCB), 
                new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsArrange, new PropertyChangedCallback(OnValueChanged)));

        public string Value
            get { return (string)GetValue(ValueProperty); }
                if (Value.Equals(value))
                SetValue(ValueProperty, value);

        public HCCB()

        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            HCCB hccb = d as HCCB;
            if (hccb == null || hccb._settingState)

            string newValue = e.NewValue as string, oldValue = e.OldValue as string;

            if (newValue == null || newValue.Equals(oldValue))

            XmlDocument document = new XmlDocument();
            document.PreserveWhitespace = false;

            XmlDataProvider dataProvider = hccb.FindResource("xmlDataProvider") as XmlDataProvider;
            dataProvider.Document = document;

        private void ComboBox_DropDownClosed(object sender, EventArgs e)
            HCCB hccb = sender as HCCB;
            if (hccb == null)

            XmlDataProvider dataProvider = hccb.FindResource("xmlDataProvider") as XmlDataProvider;
            XmlDocument document = dataProvider.Document;
            string newXml = document.OuterXml;
            if (!Value.Equals(newXml))
                    _settingState = true;
                    Value = newXml;
                    _settingState = false;

This is how a sample XML, which is set on the 'Value' property of the ComboBox looks:
<?xml version="1.0" encoding="utf-16"?>
  <child text="text1" checked="false">
    <child text="text11" checked="true" />
  <child text="text2" checked="true">
    <child text="text21" checked="false" />
    <child text="text22" checked="true" />
  <child text="text3" checked="false" >
    <child text="text33" checked="false" >
      <child text="text333" checked="true" >
        <child text="text3333" checked="false" >

Now, I want to create a parent child relation between the CheckBoxex. They are not tri-state. So, when I check a CheckBox of a TreeViewItem all the CheckBoxes of the child TreeViewItems should be checked. Same for uncheck. I referred to many sources on the net to get an idea of how that can be done. The answer by Dr. WPF in this comes the closest to what I need.
But, I am still at loss as to how will I bind the AttachedProperty of the TreeViewItem to XPath as well as an implementation to set the corresponding AttachedProperty of the child element.
This article uses the view model to achieve the same.
One option I have is to follow a combination of this and the previous article to first create a view model from the XML using LINQ. And then use that view model to do the parent child relation. Something similar is done here which uses a custom library. But, is there no other way to achieve this?

These are the articles I referred to get where I got.
. WPF-XmlDataProvider-Two-Way-Data-Binding
. Two-way binding of Xml data to the WPF TreeView
. WPF: XmlDataProvider Two-Way Data Binding
. WPF: Visualizing arbitrary XML documents in a TreeView control
Posted 14-Nov-12 5:27am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03-2016 | 2.8.180323.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100