locked
Gridview ScrollViewer Issue

    Question

  • Hi,

    So I have a gridview with some rectangles in it and I would like to manually scroll the gridview using the ScrollViewer.ChangeView method. So I trap on the right arrow key and then invoke the ChangeView method to scroll the gridview to a different location. I notice if I slowly hit the key repeatedly then the horizontal offset is what I expect it to be based on the offset parameter I passed to the ChangeView method. However, if I hit the right arrow key quickly then the horizontal offsets are not what they should be based on the parameter I passed. Is there any way I can fix this so that no matter how quickly I hit the key (or even if I hold it down) the horizontal offset of the ScrollViewer will consistently increase by the offset I pass to ChangeView?

    Thanks :-)

    Here is my example ...

    <Page
        x:Class="ReorderTest.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:ReorderTest"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
    
        <Canvas x:Name="canvas" Width="1920" Height="1080">
            <GridView x:Name="gridView1" Width="1920" Height="1080" CanReorderItems="True" SelectionMode="Single" ScrollViewer.HorizontalScrollMode="Enabled" CanDragItems="True" AllowDrop="True">
                <GridView.ItemContainerTransitions>
                    <TransitionCollection>
                        <RepositionThemeTransition/>
                    </TransitionCollection>
                </GridView.ItemContainerTransitions>
            </GridView>
        </Canvas>
    
    
    
    
    </Page>
    

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using System.Diagnostics;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    using Windows.UI.Xaml.Media.Imaging;
    using Windows.UI.Xaml.Media.Animation;
    using Windows.UI.Xaml.Shapes;
    using Windows.System;
    using Windows.UI.Core;
    using WinRTXamlToolkit.Controls.Extensions;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace ReorderTest
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
    
            private ScrollViewer scrollViewer;
    
            public MainPage()
            {
                this.InitializeComponent();
                Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
    
                SolidColorBrush greenBrush = new SolidColorBrush(Windows.UI.Colors.Green);
    
                for (int i = 0; i < 60; i++)
                {
                    Rectangle temp = new Rectangle();
    
                    temp.Height = 250;
                    temp.Width = 400;
                    temp.Fill = greenBrush;
    
                    gridView1.Items.Add(temp);
                }
                this.Loaded += MainPage_Loaded;
                
    
    
            }
    
    
            private void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                scrollViewer = gridView1.GetFirstDescendantOfType<ScrollViewer>(); // using WinRTXamlToolkit.Controls.Extensions; 
            }
    
            private void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
            {
                //Rects[1].Opacity = 0;
                if (args.VirtualKey == VirtualKey.Right)
                {
                    scrollViewer.ChangeView(scrollViewer.HorizontalOffset + 300, null, null);
                    Debug.WriteLine(scrollViewer.HorizontalOffset);
    
                }
            }
    
    
        }
    
        
    
    }
    

     

    Wednesday, February 04, 2015 5:08 PM

Answers

  • Okay here is what I have determined ... I was using the scrollViewer.HorizontalOffset property to help make the calculation for the offset to send to the ChangeView method. That property is not reliable when quickly hitting keys and firing the keydown events. I found that if I calculate the offset using a constant then I can achieve my goals.
    • Marked as answer by duffybr Friday, February 06, 2015 2:59 AM
    Friday, February 06, 2015 2:59 AM

All replies

  • Hi duffybr,

    Try something like I suggested you in the previous thread:https://social.msdn.microsoft.com/Forums/en-US/8e81da67-949a-4206-855e-1dad60299841/scrollviewerchangeview-not-working-on-my-vertically-scrolling-gridview?forum=winappswithcsharp

    Does it not working?

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 05, 2015 6:30 AM
    Moderator
  • Thanks but that does not help. For some reason even if I delay for up to a whole second the scrollview's offset does not increase by the offset parameter I am passing it when I hit the right arrow key quickly.
    Thursday, February 05, 2015 5:59 PM
  • Okay here is what I have determined ... I was using the scrollViewer.HorizontalOffset property to help make the calculation for the offset to send to the ChangeView method. That property is not reliable when quickly hitting keys and firing the keydown events. I found that if I calculate the offset using a constant then I can achieve my goals.
    • Marked as answer by duffybr Friday, February 06, 2015 2:59 AM
    Friday, February 06, 2015 2:59 AM