using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls.Primitives;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
using Telerik.Windows.Controls.GridView;

namespace ScrollingTwoGrids_Wpf_2009_Q12
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		public Window1()
		{
			InitializeComponent();

			RadGridView1.ItemsSource = this.CreateData();
			RadGridView2.ItemsSource = this.CreateData();
		}

		private object CreateData()
		{
			return
				new Message[] { 
				new Message { Sender = "tom@hanna-barbera.com", Subject = "Cats are cool", Size = 100 },
				new Message { Sender = "jerry@hanna-barbera.com", Subject = "Mice are cool", Size = 100 },
				new Message { Sender = "spike@hanna-barbera.com", Subject = "Dogs are cool", Size = 100 },
				new Message { Sender = "tom@hanna-barbera.com", Subject = "Cats are cool", Size = 100 },
				new Message { Sender = "jerry@hanna-barbera.com", Subject = "Mice are cool", Size = 100 },
				new Message { Sender = "spike@hanna-barbera.com", Subject = "Dogs are cool", Size = 100 },
				new Message { Sender = "tom@hanna-barbera.com", Subject = "Cats are cool", Size = 100 },
				new Message { Sender = "jerry@hanna-barbera.com", Subject = "Mice are cool", Size = 100 },
				new Message { Sender = "spike@hanna-barbera.com", Subject = "Dogs are cool", Size = 100 },
				new Message { Sender = "tom@hanna-barbera.com", Subject = "Cats are cool", Size = 100 },
				new Message { Sender = "jerry@hanna-barbera.com", Subject = "Mice are cool", Size = 100 },
				new Message { Sender = "spike@hanna-barbera.com", Subject = "Dogs are cool", Size = 100 }
				};
		}

		private void Grid_Loaded(object sender, RoutedEventArgs e)
		{
			ScrollBar verticalScrollBar1 = this.RadGridView1.ChildrenOfType<ScrollBar>().Where(b => b.Name == "PART_VerticalScrollBar").FirstOrDefault();
			ScrollBar horizontalScrollBar1 = this.RadGridView1.ChildrenOfType<ScrollBar>().Where(b => b.Name == "PART_HorizontalScrollBar").FirstOrDefault();
			ScrollBar verticalScrollBar2 = this.RadGridView2.ChildrenOfType<ScrollBar>().Where(b => b.Name == "PART_VerticalScrollBar").FirstOrDefault();
			ScrollBar horizontalScrollBar2 = this.RadGridView2.ChildrenOfType<ScrollBar>().Where(b => b.Name == "PART_HorizontalScrollBar").FirstOrDefault();

			verticalScrollBar1.ValueChanged += new RoutedPropertyChangedEventHandler<double>(verticalScrollBar1_ValueChanged);
			horizontalScrollBar1.ValueChanged += new RoutedPropertyChangedEventHandler<double>(horizontalScrollBar1_ValueChanged);
			verticalScrollBar2.ValueChanged += new RoutedPropertyChangedEventHandler<double>(verticalScrollBar2_ValueChanged);
			horizontalScrollBar2.ValueChanged += new RoutedPropertyChangedEventHandler<double>(horizontalScrollBar2_ValueChanged);
		}

		void horizontalScrollBar1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			(this.RadGridView2.ChildrenOfType<GridViewScrollViewer>().FirstOrDefault()).ScrollToHorizontalOffset(e.NewValue);
		}

		void verticalScrollBar1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			(this.RadGridView2.ChildrenOfType<GridViewScrollViewer>().FirstOrDefault()).ScrollToVerticalOffset(e.NewValue);
		}

		void horizontalScrollBar2_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			(this.RadGridView1.ChildrenOfType<GridViewScrollViewer>().FirstOrDefault()).ScrollToHorizontalOffset(e.NewValue);
		}

		void verticalScrollBar2_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			(this.RadGridView1.ChildrenOfType<GridViewScrollViewer>().FirstOrDefault()).ScrollToVerticalOffset(e.NewValue);
		}
	}
}
