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

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

            var attachmentsDefinition = new GridViewTableDefinition();
            attachmentsDefinition.Relation = new PropertyRelation("Attachments");
            RadGridView1.TableDefinition.ChildTableDefinitions.Add(attachmentsDefinition);

            RadGridView1.ItemsSource = new ObservableCollection<Message> { 
				new Message { Sender = "tom@hanna-barbera.com", Subject = "Cats are cool", Attachments = TestAttachments()},
				new Message { Sender = "jerry@hanna-barbera.com", Subject = "Mice are cool", Attachments = TestAttachments() },
				new Message { Sender = "spike@hanna-barbera.com", Subject = "Dogs are cool", Attachments = TestAttachments() }
			};
        }

        private ICollection<Attachment> TestAttachments()
        {
            return new ObservableCollection<Attachment>{
                new Attachment { Name = "File1.txt" },
                new Attachment { Name = "File2.txt" },
                new Attachment { Name = "File3.txt" }
            };
        }

        private void insertChild_Click(object sender, RoutedEventArgs e)
        {
            var childControls = this.RadGridView1.ChildrenOfType<GridViewDataControl>().Where(c => !(c is RadGridView));
            foreach (var control in childControls)
            {
                control.BeginInsert();
            }
        }
    }
}
