using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using Telerik.Windows.Controls;

namespace EditableComboInGridViewAddingItems_SL
{
	public partial class MainPage : UserControl
	{
		public MainPage()
		{
			InitializeComponent();

			List<Item> items = new List<Item>();
			items.Add(new Item() { FirstName = "Jane" });
			items.Add(new Item() { FirstName = "John" });

			this.RadGridView1.ItemsSource = items;

			List<String> comboItems = new List<string>();
			comboItems.Add("Jane");
			comboItems.Add("John");


			((GridViewComboBoxColumn)this.RadGridView1.Columns["ComboColumn"]).ItemsSource = comboItems;
			this.RadGridView1.CellEditEnded += new EventHandler<GridViewCellEditEndedEventArgs>(RadGridView1_CellEditEnded);
			
		}

		void RadGridView1_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
		{
			RadComboBox combo = e.Cell.ChildrenOfType<RadComboBox>().FirstOrDefault();
			List<string> comboItems =  combo.ItemsSource as List<string>;

			string  textEntered = e.Cell.ChildrenOfType<RadComboBox>()[0].Text;
			
		//	List<string> comboItems = ((GridViewComboBoxColumn)this.RadGridView1.Columns["ComboColumn"]).ItemsSource as List<string>;
			if (!comboItems.Contains(textEntered))
			{
				comboItems.Add(textEntered);
			}

			((Item)e.Cell.DataContext).FirstName = textEntered;
		}


		public class Item
		{
			public string FirstName { get; set; }
		}
	}
}
