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

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

		private void BindGridToData()
		{
			this.RadGridView1.ItemsSource = Person.GetSampleObservableCollectionOfPersons();
			this.RadGridView1.CellEditEnded += new EventHandler<Telerik.Windows.Controls.GridView.GridViewCellEditEndedEventArgs>(RadGridView1_CellEditEnded);
			((GridViewComboBoxColumn) this.RadGridView1.Columns[0]).ItemsSource = GetCountries();
		}

		void RadGridView1_CellEditEnded(object sender, Telerik.Windows.Controls.GridView.GridViewCellEditEndedEventArgs e)
		{
			
		}

		private System.Collections.IEnumerable GetCountries()
		{
			List<Country> countries = new List<Country>();
			countries.Add(new Country(){ID=0, Name = "USA"});
			countries.Add(new Country(){ID=1, Name = "USSR"});
			countries.Add(new Country(){ID=2, Name = "Italy"});
			return countries;
		}
	}

	public class Country
	{
		public int ID { get; set; }
		public string Name { get; set; }
	}

	public class Person : INotifyPropertyChanged
	{
		private string firstName;
		private string lastName;
		private int countryID;

		public event PropertyChangedEventHandler PropertyChanged;

		public Person(int countryID, string firstName, string lastName)
		{
			this.countryID = countryID;
			this.firstName = firstName;
			this.lastName = lastName;
		}

		public int CountryID
		{
			get
			{
				return this.countryID;
			}
			set
			{
				if (this.countryID != value)
				{
					this.countryID = value;
					this.OnPropertyChanged("Age");
				}
			}
		}

		public string FirstName
		{
			get
			{
				return this.firstName;
			}
			set
			{
				if (this.firstName != value)
				{
					this.firstName = value;
					this.OnPropertyChanged("FirstName");
				}
			}
		}

		public string LastName
		{
			get
			{
				return this.lastName;
			}
			set
			{
				if (this.lastName != value)
				{
					this.lastName = value;
					this.OnPropertyChanged("LastName");
				}
			}
		}

		public static ObservableCollection<Person> GetSampleObservableCollectionOfPersons()
		{
			var persons = new ObservableCollection<Person>
			              	{
			              		new Person(0, "John", "Smith"),
			              		new Person(1, "Jane", "Smith"),
			              		new Person(2, "Peter", "Smith")
			              	};
			return persons;
		}

		private void OnPropertyChanged(string property)
		{
			if (this.PropertyChanged != null)
				this.PropertyChanged(this, new PropertyChangedEventArgs(property));
		}
	}
}
