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


namespace Cell_RowSelection
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();		
			
			this.playersGrid.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(playersGrid_PreviewMouseLeftButtonDown);        
		}

		void playersGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			var element = e.OriginalSource as FrameworkElement;
			var cell = element.ParentOfType<GridViewCell>();

			if (cell != null)
			{
				this.playersGrid.SelectionUnit = GridViewSelectionUnit.Cell;
			}
		}
		      				
		private void playersGrid_RowLoaded(object sender, RowLoadedEventArgs e)
		{
			GridViewRow row = e.Row as GridViewRow;
			if (row != null)
			{	
				Border indicatorPresenter = row.ChildrenOfType<Border>().Where(b => b.Name == "PART_IndicatorPresenter").FirstOrDefault();
				indicatorPresenter.MouseLeftButtonDown += new MouseButtonEventHandler(indicatorPresenter_MouseLeftButtonDown);								
			}			
		}

		void indicatorPresenter_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{	
			this.playersGrid.SelectionUnit = GridViewSelectionUnit.FullRow;
		}		
		
	}
}
