using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using Telerik.Windows.Controls;

namespace DynamicComboBoxes_SL
{
	public partial class MainPage : UserControl
	{
		public MainPage()
		{
			InitializeComponent();
			this.RadGridView1.ItemsSource = GetLocations();
			((GridViewComboBoxColumn)this.RadGridView1.Columns[0]).ItemsSource = GetContinents();
		}

		private List<Continent> GetContinents()
		{
			List<Continent> continents = new List<Continent>();
			continents.Add(new Continent() { ID = 0, Name = "Europe" });
			continents.Add(new Continent() { ID = 1, Name = "Asia" });
			continents.Add(new Continent() { ID = 2, Name = "Afrika" });

			return continents;
		}

		private List<Location> GetLocations()
		{
			List<Location> locations = new List<Location>();

			locations.Add(new Location() { ContinentID = 0, CountryID = 0 });
			locations.Add(new Location() { ContinentID = 1, CountryID = 0 });
			locations.Add(new Location() { ContinentID = 2, CountryID = 0 });

			return locations;
		}
	}

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

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

	public class Location : INotifyPropertyChanged
	{
		private int continentID;
		public int ContinentID
		{
			get
			{
				return this.continentID;
			}
			set
			{
				this.continentID = value;
				OnPropertyChanged("ContinentId");
				this.countries = GetCountriesForContinent(value);
				OnPropertyChanged("Countries");
			}
		}

		private List<Country> GetCountriesForContinent(int continentID)
		{
			List<Country> countries = new List<Country>();

			if (continentID == 0)
			{
				countries.Add(new Country() { ID = 0, Name = "Italy" });
				countries.Add(new Country() { ID = 1, Name = "France" });
				countries.Add(new Country() { ID = 2, Name = "Germany" });
			}
			else if (continentID == 1)
			{
				countries.Add(new Country() { ID = 0, Name = "China" });
				countries.Add(new Country() { ID = 1, Name = "India" });
				countries.Add(new Country() { ID = 2, Name = "Pakistan" });

			}
			else if (continentID == 2)
			{
				countries.Add(new Country() { ID = 0, Name = "Nigeria" });
				countries.Add(new Country() { ID = 1, Name = "Egypt" });
				countries.Add(new Country() { ID = 2, Name = "South Africa" });
			}

			return countries;
		}

		private int countryID;
		public int CountryID
		{
			get
			{
				return this.countryID;
			}
			set
			{
				if (this.countryID != value)
				{
					this.countryID = value;
					OnPropertyChanged("CountryId");
				}
			}
		}
		private List<Country> countries;
		public List<Country> Countries
		{
			set
			{
				this.countries = value;
				OnPropertyChanged("Countries");
			}
			get
			{
				return this.countries;
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

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

	}
}
