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.Collections.ObjectModel;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;

namespace WpfApplication16
{
    
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static MyViewModel vm = new MyViewModel();

        public MainWindow()
        {
            InitializeComponent();
            layoutRoot.DataContext = vm;
        }

        private void HierarchyButton_Checked(object sender, RoutedEventArgs e)
        {
            if (NoHierarchyButton.IsChecked == true)
                vm.Level = State.No;
            else if (L1HierarchyButton.IsChecked == true)
                vm.Level = State.L1;
            else
                vm.Level = State.L2;

            testGrid.Rebind();
        }
    }

    public class HierarchyTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (MainWindow.vm.Level == State.No)
                return NoHierarchyTemplate;
            else if (MainWindow.vm.Level == State.L1)
                return L1HierarchyTemplate;
            else
                return L2HierarchyTemplate;
        }

        public DataTemplate NoHierarchyTemplate { get; set; }
        public DataTemplate L1HierarchyTemplate { get; set; }
        public DataTemplate L2HierarchyTemplate { get; set; }

        public HierarchyTemplateSelector()
        {
            NoHierarchyTemplate = null;
        }
    }

    public class HierarchyButtonStyleSelector : StyleSelector
    {
        public override Style SelectStyle(object item, DependencyObject container)
        {
			if (MainWindow.vm.Level == State.No)
			{
				Style st = new Style(typeof(GridViewToggleButton));
				st.Setters.Add(new Setter(GridViewToggleButton.IsEnabledProperty, false));
				return st;
			}
			else
			{
				Style st = new Style(typeof(GridViewToggleButton));
				st.Setters.Add(new Setter(GridViewToggleButton.IsEnabledProperty, true));
				return st;
			}
        }
    }

    public enum State
    {
        No,
        L1,
        L2
    }

    public class MyViewModel
    {
        public ObservableCollection<Level1Item> l1Items { get; set; }
        public State Level { get; set; }

        public MyViewModel()
        {
            Level = State.No;
            l1Items = new ObservableCollection<Level1Item>();
            l1Items.Add(new Level1Item());
            l1Items.Add(new Level1Item());
            l1Items.Add(new Level1Item());
        }
    }

    public class Level1Item
    {
        Random rnd = new Random();
        public string Name { get; set; }
        public ObservableCollection<Level2Item> l2Items { get; set; }
        public Level1Item()
        {
            l2Items = new ObservableCollection<Level2Item>();
            Name = String.Format("Item1_{0}", rnd.Next());
            for (int i = 0; i < 10; i++)
            {
                l2Items.Add(new Level2Item());
            }
        }
    }

    public class Level2Item
    {
        public string Name { get; set; }
        public ObservableCollection<Level3Item> l3Items { get; set; }

        public Level2Item()
        {
            l3Items = new ObservableCollection<Level3Item>();
            Random rnd = new Random();
            Name = String.Format("Item2_{0}", rnd.Next());
            for (int i = 0; i < 10; i++)
            {
                l3Items.Add(new Level3Item());
            }
        }
    }

    public class Level3Item
    {
        public string Name { get; set; }
        public Level3Item()
        {
            Random rnd = new Random();
            Name = String.Format("Item3_{0}", rnd.Next());
        }
    }
}
