Home > WPF > Set Scroll to Top DataGrid or ListView in WPF

Set Scroll to Top DataGrid or ListView in WPF

You can set the scroll of DataGrid or ListView to top programmatically. Here is the code

  ScrollViewer scrollViewer = GetScrollViewer(gridViewName) as ScrollViewer;
scrollViewer.ScrollToHome();

public static DependencyObject GetScrollViewer(DependencyObject o)
{
if (o is ScrollViewer)
{ return o; }

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

var result = GetScrollViewer(child);
if (result == null)
{
continue;
}
else
{
return result;
}
}

return null;
}

Happy coding

Advertisements
Categories: WPF
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: