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

namespace AddingNewDataItem
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page
    {
        PopupControl popup = new PopupControl();
        RadWindow wnd = new RadWindow();
        public Page1()
        {
            InitializeComponent();
            wnd.Content = popup;
           
        }

        private void playersGrid_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
        {
            e.Cancel = true;
            popup.DataContext = new Player();
            wnd.Show();
            wnd.Closed += new System.EventHandler<WindowClosedEventArgs>(wnd_Closed);
        }
        void wnd_Closed(object sender, WindowClosedEventArgs e)
        {
            RadWindow window = sender as RadWindow;

            if (window.DialogResult == true)
            {
                Player player = popup.DataContext as Player;
                player.Country = "Bulgaria";
                var players = this.playersGrid.ItemsSource as IList<Player>;
                if (!players.Contains(player))
                {
                    players.Add(player);
                }

                this.playersGrid.ScrollIntoView(player);
                this.playersGrid.SelectedItem = player;
            }
            else
            {
                Player player = popup.DataContext as Player;
                var players = this.playersGrid.ItemsSource as IList<Player>;
                players.Remove(player);

            }

        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            popup.DataContext = new Player();
            wnd.Show();
            wnd.Closed += new System.EventHandler<WindowClosedEventArgs>(wnd_Closed);
        }
    }
}