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 Telerik.Windows.Controls;

namespace WPF_TreeView_BindCheckState
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void treeView_Checked(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            RadTreeViewItem checkedItem = e.OriginalSource as RadTreeViewItem;
            if (checkedItem != null)
            {
                foreach (RadTreeViewItem child in checkedItem.Items)
                {
                    if (child.CheckState != System.Windows.Automation.ToggleState.On)
                    {
                        child.CheckState = System.Windows.Automation.ToggleState.On;
                    }
                }
            }
        }
    }

    public class SourceItem
    {
        public SourceItem()
        {
            this.Items = new List<SourceItem>();
        }

        public System.Windows.Automation.ToggleState CheckState;
        public string Header;
        public List<SourceItem> Items;
    }

    public class SourceCollection : List<SourceItem>
    {
        public SourceCollection()
        {
            SourceItem item = new SourceItem() { Header = "Item 1", CheckState = System.Windows.Automation.ToggleState.Off };
            item.Items.Add(new SourceItem() { Header = "Item 1.1", CheckState = System.Windows.Automation.ToggleState.Off });
            item.Items.Add(new SourceItem() { Header = "Item 1.2", CheckState = System.Windows.Automation.ToggleState.Off });
            item.Items.Add(new SourceItem() { Header = "Item 1.3", CheckState = System.Windows.Automation.ToggleState.Off });

            this.Add(item);
            this.Add(new SourceItem() { Header = "Item 2", CheckState = System.Windows.Automation.ToggleState.Off });
            this.Add(new SourceItem() { Header = "Item 3", CheckState = System.Windows.Automation.ToggleState.Off });
            this.Add(new SourceItem() { Header = "Item 4", CheckState = System.Windows.Automation.ToggleState.On });
            this.Add(new SourceItem() { Header = "Item 5", CheckState = System.Windows.Automation.ToggleState.On });
        }
    }

    public class TreeViewDataSource : List<RadTreeViewItem>
    {
        public TreeViewDataSource()
        {
            SourceCollection source = new SourceCollection();

            foreach (SourceItem item in source)
            {
                this.Add(LoadTreeViewItem(item));
            }
        }

        private RadTreeViewItem LoadTreeViewItem(SourceItem item)
        {
            RadTreeViewItem result = new RadTreeViewItem() { Header = item.Header, CheckState = item.CheckState };

            if (item.Items.Count > 0)
            {
                foreach (SourceItem child in item.Items)
                {
                    result.Items.Add(LoadTreeViewItem(child));
                }
            }

            return result;
        }
    }
}
