using System;
using System.Linq;
using System.Windows;
using Telerik.Windows.Data;
using System.Collections;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Collections.Generic;
using Telerik.Windows.Controls;
using Telerik.Windows;
using CascadingComboBoxColumns;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<Location> locations = new List<Location>();

            locations.Add(new Location() { ContinentCode = "EU", CountryID = 2 });


            ((GridViewComboBoxColumn)this.radGridView.Columns[0]).ItemsSource = Locations.Continents;
            this.radGridView.ItemsSource = locations;

            //We need to sense when the combo selection is changed and submit the value immediately
            //otherwise the value will be submited on leaving the cell which is too late for our needs. 
            this.AddHandler(RadComboBox.SelectionChangedEvent, new System.Windows.Controls.SelectionChangedEventHandler(comboSelectionChanged));
        }

        void comboSelectionChanged(object sender, RoutedEventArgs args)
        {
            RadComboBox comboBox = (RadComboBox)args.OriginalSource;

            if (comboBox.SelectedValue == null
                || comboBox.SelectedValuePath != "Code") // we take action only if the continent combo is changed
                return;

            Location location = comboBox.DataContext as Location;
            location.ContinentCode = (string)comboBox.SelectedValue;//we submit the value immediately rather than waiting the cell to lose focus.
        }
    }


    public class Location : INotifyPropertyChanged
    {

        private string continentCode;
        public string ContinentCode
        {
            get
            {
                return this.continentCode;
            }
            set
            {
                if (value != this.continentCode)
                {
                    this.continentCode = value;
                    this.OnPropertyChanged("ContinentCode");

                    if(this.continentCode == "NA")
                    {
                        this.IsReadOnly = true;
                    }
                    else
                    {
                        this.IsReadOnly = false;
                    }
                    this.CountryID = null;
                }
            }
        }
        private int? countryID;
        public int? CountryID
        {
            get
            {
                return this.countryID;
            }
            set
            {
                this.countryID = value;
                this.OnPropertyChanged("CountryID");
            }
        }

        private bool isReadOnly;

        public bool IsReadOnly
        {
            get { return isReadOnly; }
            set
            {
                this.isReadOnly = value;
                this.OnPropertyChanged("IsReadOnly");
            }
        }


        public IEnumerable<Country> AvailableCountries
        {
            get
            {
                return from c in Locations.Countries
                       where c.ContinentCode == this.ContinentCode
                       select c;
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

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

        #endregion
    }
}
