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

Get Visible Rows in RadGridView

Environment

Product RadGridView for WPF
Version 2024.3.821

Description

How to get the visible rows (GridViewRow) containers of RadGridView in the viewport.

Solution

To get the visible rows, you can first use the ChildrenOfType extension method. This will allow you to access the generated GridViewRow containers. These are the containers within the viewport, plus couple more below and above the viewport.

Then, you can iterate the rows collection and get only the rows which bounds are contained in the bounds of the parent GridViewVirtualizingPanel.

 private void GetVisibleRowsButton_Click(object sender, RoutedEventArgs e) 
 { 
     var panel = this.gridView.FindChildByType<GridViewVirtualizingPanel>(); 
     var visibleRows = this.gridView.ChildrenOfType<GridViewRow>().Where(row => IsRowInViewport(panel, row));             
 } 
 
 bool IsRowInViewport(GridViewVirtualizingPanel panel, UIElement element) 
 { 
     var transform = element.TransformToVisual(panel); 
     var startPosition = transform.Transform(new Point(0, 0)); 
 
     var elementBounds = new Rect(startPosition, new Size(1, element.DesiredSize.Height)); 
     var viewport = new Rect(new Point(0, 0), new Size(panel.ViewportWidth, panel.ViewportHeight)); 
 
     bool result = viewport.Contains(elementBounds); 
     return result; 
 } 
In this article