using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;

namespace ScrollProblemTest
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            List<Item> items = new List<Item>();

            for (int i = 0; i < 100; i++)
            {
                items.Add(new Item()
                {
                    Property1 = i,
                    Property2 = i * 2,
                    Property3 = i * 3,
                    Property4 = i * 4,
                    Property5 = i * 5,
                    Property6 = i * 6,
                    Property7 = i * 7,
                    Property8 = i * 8,
                    Property9 = i * 9
                });
            }

            this.RadGridView1.ItemsSource = items;
            this.RadGridView1.DataLoaded += new EventHandler<EventArgs>(RadGridView1_DataLoaded);
            this.RadGridView1.SelectionChanged += new EventHandler<SelectionChangeEventArgs>(RadGridView1_SelectionChanged);
            
        }

        void RadGridView1_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            var viewer = this.RadGridView1.ChildrenOfType<GridViewScrollViewer>()[0];
            viewer.ScrollToHorizontalOffset(this.HorizontalOffsetCached);
        }

        void RadGridView1_DataLoaded(object sender, EventArgs e)
        {
            this.RadGridView1.DataLoaded -= new EventHandler<EventArgs>(RadGridView1_DataLoaded);
            var viewer = this.RadGridView1.ChildrenOfType<GridViewScrollViewer>()[0];
            viewer.ScrollChanged += new ScrollChangedEventHandler(viewer_ScrollChanged);
        }

        double HorizontalOffsetCached;
        void viewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            var viewer = sender as GridViewScrollViewer;
            if (viewer.HorizontalOffset != 0)
                this.HorizontalOffsetCached = viewer.HorizontalOffset;
            else if (e.HorizontalChange == this.HorizontalOffsetCached * -1 && e.VerticalChange==0)
            {
                Dispatcher.BeginInvoke(() =>
                {
                    viewer.ScrollToHorizontalOffset(this.HorizontalOffsetCached);
                });
            }
        }

        void ScrollChanged(object Sender, RoutedEventArgs args)
        {

        }
    }

    public class Item
    {
        public int Property1 { get; set; }
        public int Property2 { get; set; }
        public int Property3 { get; set; }
        public int Property4{ get; set; }
        public int Property5 { get; set; }
        public int Property6 { get; set; }
        public int Property7 { get; set; }
        public int Property8 { get; set; }
        public int Property9 { get; set; }
    }
}
