using System;
using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;

namespace RadGridView_SL4_AR_2
{
	public partial class MainPage : UserControl
	{
		public MainPage()
		{
			InitializeComponent();
			this.clubsGrid.CellValidated += new EventHandler<GridViewCellValidatedEventArgs>(clubsGrid_CellValidated);
		}

		void clubsGrid_CellValidated(object sender, GridViewCellValidatedEventArgs e)
		{
			this.validationSummary.Errors.Clear();		
			GridViewCell errorCell = (e.Cell.ParentRow as GridViewRow).GetCellFromPropertyName(e.ValidationResult.PropertyName);
			ValidationSummaryItemSource validationSummaryItemSource = new ValidationSummaryItemSource(e.ValidationResult.PropertyName, errorCell);
			this.validationSummary.Errors.Add(new ValidationSummaryItem(e.ValidationResult.ErrorMessage, string.Empty,
																		ValidationSummaryItemType.ObjectError,
																		validationSummaryItemSource, (e.Cell.ParentRow as GridViewRow).DataContext));
		}

		private void ValidationSummary_FocusingInvalidControl(object sender, FocusingInvalidControlEventArgs e)
		{
			GridViewCell cell = e.Target.Control as GridViewCell;
			if (cell != null)
			{
				cell.BeginEdit();
				e.Target = new ValidationSummaryItemSource(e.Target.PropertyName, cell.GetEditingElement() as Control);
			}
		}

		private void clubsGrid_RowValidated(object sender, GridViewRowValidatedEventArgs e)
		{
			this.validationSummary.Errors.Clear();			
		}
	}
}
