using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using C1.Silverlight.DataGrid;
using System.Collections.Generic;
using System;
using System.Linq;
using C1.Silverlight;
using System.ComponentModel;

namespace C1DataGrid_RelatedComboColumns
{
    public partial class Page : UserControl
    {
        DataGridComboBoxColumn _colCountry = new DataGridComboBoxColumn();
        DataGridComboBoxColumn _colRegion = new DataGridComboBoxColumn();

        public Page()
        {
            InitializeComponent();

            // handle auto generation to set the combo columns
            grid.AutoGeneratingColumn += new EventHandler<DataGridAutoGeneratingColumnEventArgs>(grid_AutoGeneratingColumn);

            // update combo collections while the values are edited
            grid.BeganEdit += new EventHandler<DataGridBeganEditEventArgs>(grid_BeganEdit);

            // load grid
            grid.ItemsSource = Data.GenerateData();
        }

        void grid_BeganEdit(object sender, DataGridBeganEditEventArgs e)
        {
            if (e.Column == _colRegion)
            {
                var combo = e.EditingElement as C1ComboBox;
                var userInfo = e.Row.DataItem as UserInfo;
                var country = Country.GetCountryById(userInfo.CountryId);

                // restrict combo collection
                combo.ItemsSource = country.Regions;
            }
        }


        private void grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Property.Name == "CountryId")
            {
                _colCountry.DisplayMemberPath = "Name";
                _colCountry.SelectedValuePath = "Id";
                _colCountry.SortMemberPath = "CountryId";
                _colCountry.FilterMemberPath = "CountryId";
                _colCountry.Header = "Country";
                _colCountry.Binding = new Binding() { Path = new PropertyPath("CountryId"), Mode = BindingMode.TwoWay };
                _colCountry.ItemsSource = Country.AllCountries;
                e.Column = _colCountry;
            }
            else if (e.Property.Name == "RegionId")
            {
                _colRegion.DisplayMemberPath = "Name";
                _colRegion.SelectedValuePath = "Id";
                _colRegion.SortMemberPath = "RegionId";
                _colRegion.FilterMemberPath = "RegionId";
                _colRegion.Header = "Region";
                _colRegion.Binding = new Binding() { Path = new PropertyPath("RegionId"), Mode = BindingMode.TwoWay };
                _colRegion.ItemsSource = Region.AllRegions;
                e.Column = _colRegion;
            }
        }
    }
}
