Click here to Skip to main content
15,904,652 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am practicing with datagrid, when I have many rows, the vertical scrollbar is activated. How to know that I am moving the scrollbar button from top to bottom with the left mouse button? Happy new year everybody.

What I have tried:

I'm new with WPF, I dont'n know how to do.
Updated 4-Jan-23 13:40pm
[no name] 4-Jan-23 16:03pm

1 solution

Further to Gerry's solution link, I am expecting you to ask how to get the visible Rows. Here is a solution from Wpf DataGrid: how to detect the set of visible cells?
Wire up the event:
<DataGrid ScrollViewer.ScrollChanged="DataGrid_ScrollChanged">

Now the event handler:
private static T GetChildOfType<T>(DependencyObject depObj)
	where T : DependencyObject
	if (depObj == null) return null;

	for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
		var child = VisualTreeHelper.GetChild(depObj, i);

		var result = (child as T) ?? GetChildOfType<T>(child);
		if (result != null) return result;
	return null;

private void DataGrid_ScrollChanged(object sender, ScrollChangedEventArgs e)
	DataGrid datagrid = sender as DataGrid;
	var scroll = GetChildOfType<ScrollViewer>((DependencyObject)sender);

	int firstRow = (int)scroll.VerticalOffset;
	int lastRow = (int)scroll.VerticalOffset + (int)scroll.ViewportHeight + 1;

	for (int i = firstRow; i < lastRow; i++)
		// do work here
Share this answer
Member 15639943 4-Jan-23 20:42pm    
Thanks a lot for your time and help. I'm going to do the corresponding tests, but I'm going to ask another question explaining the problem better (I hope)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900