Scroll Selected in Code Item to top of RadGridView

Environment

Product RadGridView for WPF

Description

How to scroll a programmatically selected item to the top.

Solution

Create an attached behavior that handles the SelectionChanged event of the RadGridView. Access the GridViewsScrollViewer element using the ChildrenOfType extesion method. Using its VerticalOffset and ViewportHeight properties determine whether the SelectedItem is within the viewport. If it is not, the selection was made from code and you can use the ScrollToVerticalOffset method in order to scroll the selected item to the top of the RadGridView.

public class RadGridView_ScrollSelectedIntoViewBehaviour 
{ 
    public static readonly DependencyProperty ScrollSelectedIntoViewProperty = 
        DependencyProperty.Register("ScrollSelectedIntoView", typeof(bool), typeof(RadGridView), new FrameworkPropertyMetadata(ScrollSelectedIntoViewPropertyChanged)); 
 
    public static bool GetScrollSelectedIntoView(DependencyObject obj) 
    { 
        return (bool)obj.GetValue(ScrollSelectedIntoViewProperty); 
    } 
 
    public static void SetScrollSelectedIntoView(DependencyObject obj, bool value) 
    { 
        obj.SetValue(ScrollSelectedIntoViewProperty, value); 
    } 
 
    private static void ScrollSelectedIntoViewPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
        RadGridView grid = d as RadGridView; 
        var shouldScrollSelectedItemIntoView = (bool)e.NewValue; 
        if (grid != null && shouldScrollSelectedItemIntoView) 
        { 
            grid.SelectionChanged += Grid_SelectionChanged; 
        } 
    } 
 
    private static void Grid_SelectionChanged(object sender, SelectionChangeEventArgs e) 
    { 
        RadGridView grid = sender as RadGridView; 
 
        object selectedItem = grid.SelectedItem; 
 
        if (selectedItem != null) 
        { 
            var scrollOffset = grid.Items.IndexOf(selectedItem) * grid.RowHeight; 
            var scrollViewer = grid.ChildrenOfType<GridViewScrollViewer>().First(); 
            var verticalOffset = scrollViewer.VerticalOffset; 
            var viewportHeight = scrollViewer.ViewportHeight; 
 
            if (scrollOffset < verticalOffset || scrollOffset > (verticalOffset + viewportHeight)) 
            { 
                scrollViewer.ScrollToVerticalOffset(scrollOffset); 
            } 
        } 
    } 
} 

<telerik:RadGridView local:RadGridView_ScrollSelectedIntoViewBehaviour.ScrollSelectedIntoView="True" /> 

Note, that the namespace "local" refers to the namespace where the RadGridView_ScrollSelectedIntoViewBehaviour class is defined.

See Also

In this article
Not finding the help you need? Improve this article