This is a migrated thread and some comments may be shown as answers.

RadGridView scrolls to top for no obvious reason.

1 Answer 231 Views
GridView
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 27 Jul 2012, 04:46 PM
Hello,

I am using RadControls version 2012.2.607.40 for WPF 4.

I am having an issue with the scrolling of a RadGridView. The problem is that under certain circumstances, the RadGridView scrolls to the top for no apparent reason. The following xaml and code-behind show a simple sample:

<Window x:Class="RadGridViewScrollingSample.MainWindow"
        xmlns:local="clr-namespace:RadGridViewScrollingSample"
        x:Name="_this"
        Title="MainWindow" Height="350">
     
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </Window.Resources>
     
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
 
        <StackPanel Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2">
            <TextBlock Text='Scroll down in the grid view below then move the mouse over "Place Mouse Here".'/>
            <TextBlock Text="You will see the grid view automatically scroll to the top."/>
            <TextBlock Text='Clicking in "Place Mouse Here" will scroll the last row of the grid view in to view, but'/>
            <TextBlock Text='the grid view will again scroll to the top when the mouse is moved outside "Place Mouse Here".'/>
        </StackPanel>
 
        <telerik:RadGridView x:Name="gridView" Grid.Row="1" Grid.Column="0" ScrollViewer.VerticalScrollBarVisibility="Visible" ItemsSource="{Binding Path=Things}"/>
         
        <TextBlock Grid.Row="1" Grid.Column="1" x:Name="placeholder" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" TextAlignment="Center" Text="Place Mouse Here" FontSize="40" MouseUp="OnMouseUp"/>
             
        <DockPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
            <TextBlock Text="This is placeholder text.  "/>
            <TextBlock Text='This text appears when the mouse is over "Place Mouse Here".' Visibility="{Binding ElementName=placeholder, Path=IsMouseOver, Converter={StaticResource BooleanToVisibilityConverter}}"/>
        </DockPanel>
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
 
namespace RadGridViewScrollingSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<Thing> things = new ObservableCollection<Thing>();
 
        private string[] colors = new string[] { "red", "green", "blue", "orange", "purple" };
        private string[] sizes = new string[] { "small", "medium", "large" };
 
        public MainWindow()
        {
            InitializeComponent();
 
            DataContext = this;
 
            int i = 0;
            for (char c = 'A'; c <= 'Z'; c++, i++)
            {
                things.Add(new Thing(c.ToString(), colors[i % colors.Length], sizes[i % sizes.Length]));
            }
        }
 
        public ObservableCollection<Thing> Things
        {
            get { return things; }
        }
 
        private void OnMouseUp(object sender, MouseButtonEventArgs e)
        {
            gridView.ScrollIntoView(things.FirstOrDefault(t => t.Name == "Z"));
        }
    }
 
    public class Thing
    {
        private string name;
        private string color;
        private string size;
 
        public Thing(string name, string color, string size)
        {
            this.name = name;
            this.color = color;
            this.size = size;
        }
 
        public string Name
        {
            get { return name; }
        }
 
        public string Color
        {
            get { return color; }
        }
 
        public string Size
        {
            get { return size; }
        }
    }
}

If I scroll the RadGridView to the bottom and then move the mouse over the text "Place Mouse Here", the RadGridView scrolls to the top. If I then click the mouse over "Place Mouse Here", causing the RadGridView to scroll to the bottom, and then move the mouse outside the text "Place Mouse Here", the RadGridView again scrolls to the top. Note that placing the mouse over the text "Place Mouse Here" causes a TextBlock to become visible at the bottom of the screen.

I can stop this auto scrolling of the RadGridView by changing either or both the grid row height of * or grid column width of * to a fixed value. However, this is not an acceptable solution because I want those grid row and column to take all remaining space in the window.

I believe, but can't prove, that this behavior has something to do with a SizedChange event being fired when the TextBlock at the bottom of the window changes its visibility. But obviously in my case, no sizes are actually changing.

Can anybody tell me what is causing the unsolicited scrolling of my RadGridView and, better yet, how to avoid this behavior?

Thank you,

-- john

1 Answer, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 30 Jul 2012, 12:16 PM
Hi John,

The thing is that the grid is placed in a row measured with Star (*) height. Thus once the height of the item beneath (DockPanel) is changed as a result of the visibility changed of the TextBlock, the size of the grid is reevaluated and its scroll bar position is reset.
What you can try is to define height for the row definition. 

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
John
Top achievements
Rank 1
Answers by
Maya
Telerik team
Share this question
or