New to Telerik UI for WPF? Download free 30-day trial

Programmatically Binding Selected Item Into View in RadGridView

Environment

Property Value
Product Progress® Telerik® UI for WPF
Version 2024.1.312

Description

How to automatically scroll to the selected row in RadGridView for WPF.

Solution

To do that, you can use the SelectionChanged event of RadGridView. In the event handler, you can access the GridViewScrollViewer control and call its ScrollToVerticalOffset. The offset can be calculated based on the index of the selected row multiplied by the RowHeight setting of the data grid.

This approach can be wrapped in an attached property that can be re-used in multiple RadGridView instances.

public class GridViewUtilities 
{ 
    public static readonly DependencyProperty ScrollSelectedIntoViewProperty = 
        DependencyProperty.RegisterAttached("ScrollSelectedIntoView", typeof(bool), typeof(GridViewUtilities), new FrameworkPropertyMetadata(false, 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) 
    { 
        var gridView = (RadGridView)d; 
        if ((bool)e.NewValue) 
        {        
            gridView.SelectionChanged += RadGridView_SelectionChanged; 
        } 
        else  
        { 
            gridView.SelectionChanged -= RadGridView_SelectionChanged; 
        } 
    } 
 
    private static void RadGridView_SelectionChanged(object sender, SelectionChangeEventArgs e) 
    {     
        var gridView = (RadGridView)sender;          
        if (gridView.SelectedItem != null) 
        { 
            var scrollOffset = gridView.Items.IndexOf(gridView.SelectedItem) * gridView.RowHeight; 
            var scrollViewer = gridView.ChildrenOfType<GridViewScrollViewer>().First(); 
            var svo = scrollViewer.VerticalOffset; 
            var svh = scrollViewer.ViewportHeight; 
 
            if(scrollOffset < svo || scrollOffset > (svo + svh)) 
            { 
                scrollViewer.ScrollToVerticalOffset(scrollOffset); 
            } 
            else 
            { 
                scrollViewer.ScrollToVerticalOffset(svo); 
            }              
        } 
    }  
} 

<telerik:RadGridView local:GridViewUtilities.ScrollSelectedIntoView="True"/> 
In this article