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 WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        bool flag = false;

        public Window1()
        {
            InitializeComponent();

            DataContext = from i in Enumerable.Range(0, 3)
                          select new MyObject()
                          {
                              ID = i,
                              Name = String.Format("Name{0}", i),
                              Items = from j in Enumerable.Range(0, 3)
                                      select new MyObject()
                                      {
                                          ID = j,
                                          Name = String.Format("Name{0}", j),
                                          Items = from k in Enumerable.Range(0, 3)
                                                  select new MyObject()
                                                  {
                                                      ID = k,
                                                      Name = String.Format("Name{0}", k)
                                                  }
                                      }
                          };
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            flag = true;

            RadGridView1.ExpandAllHierarchyItems();
        }

        private void RadGridView1_DataLoading(object sender, Telerik.Windows.Controls.GridView.GridViewDataLoadingEventArgs e)
        {
            var dataControl = (GridViewDataControl)sender;
            if (dataControl.ParentRow != null)
            {
                dataControl.Loaded -= dataControl_Loaded;
                dataControl.Loaded += dataControl_Loaded;
            }
        }

        void dataControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (flag)
            {
                ((GridViewDataControl)sender).ExpandAllHierarchyItems();
            }
        }

        private void RadGridView1_RowLoaded(object sender, RowLoadedEventArgs e)
        {
            var row = e.Row as GridViewRow;
            if (row != null)
            {
                var toggleButton = row.ChildrenOfType<GridViewToggleButton>().FirstOrDefault();
                if (toggleButton != null)
                {
                    toggleButton.Click -= new RoutedEventHandler(toggleButton_Click);
                    toggleButton.Click += new RoutedEventHandler(toggleButton_Click);
                }
            }
        }

        void toggleButton_Click(object sender, RoutedEventArgs e)
        {
            flag = false;
        }
    }

    public class MyObject
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public IEnumerable<MyObject> Items { get; set; }
    }

}
