[WPF][DATAGRID] DataGrid scroll problem with dynamic load od elements

Pascal 21 Reputation points
2021-01-14T12:18:01.623+00:00

Hello,

I would like to use a Datagrid to load data by groups of 1000 elements. Each time the scrollbar is at the bottom , I load 1000 new elements.
It works until I load my first 1000 new elements. The thumb of the scrollBar don't go up but still at the bottom. So I have to go up and down to load 1000 new elements.
I override the ScrollViewer::OnScrollChanged() to see what going on and I don't understand the result. My VerticalOffset increase by 1000 elements without any scoll!

Here some logs of the contrent of ScrollViewer::OnScrollChanged(ScrollChangedEventArgs e)
STEP 1 => Initialisation with 1000 elements
Info e.ExtentHeightChange: 0
Info e.VerticalOffset: 0
Info e.ExtentHeight: 1000
Info e.VerticalChange: 0
Info e.ViewportHeight: 18
Info e.ViewportHeightChange: 0

STEP 2 => Scroll to th bottom of the Scrollbar
Info e.ExtentHeight: 1000
Info e.ExtentHeightChange: 0
Info e.VerticalOffset: 983
Info e.VerticalChange: 81,1970343742979
Info e.ViewportHeight: 17
Info e.ViewportHeightChange: -1

STEP 2 =>the load of new data happen => Add 1000 elements in Datagrid
Info e.ExtentHeight: 2000
Info e.ExtentHeightChange: 1000
Info e.VerticalOffset: 1983 <= My Problem
Info e.VerticalChange: 1000
Info e.ViewportHeight: 17
Info e.ViewportHeightChange: 0

What i expected here is that the verticalOffset stays at 983 and the thumb of my scroll bar goes up by itself

STEP 3 : I scroll a little up and down => Add 1000 elements in dataGrid
Info e.ExtentHeight: 3000
Info e.ExtentHeightChange: 1000
Info e.VerticalOffset: 2983 <= Same Problem
Info e.VerticalChange: 1000
Info e.ViewportHeight: 17
Info e.ViewportHeightChange: 0

STEP 4 : I scroll a little up and down =>Add 1000 elements in dataGrid
Info e.ExtentHeight: 4000
Info e.ExtentHeightChange: 1000
Info e.VerticalOffset: 3983 <= Same Problem
Info e.VerticalChange: 1000
Info e.ViewportHeight: 17
Info e.ViewportHeightChange: 0

I must do scroll up dans down each time, it isn't ergonomic.
How can keep my VerticalOffset at the same value until I scroll ?

Note : I load my data by set of 1000 elements for performance and ergonomic reason. The total of data could be 300 000 elemnts or more

If someone has a solution, it would be very helpful.
Thanks for your help and your time

Pascal

Developer technologies Windows Presentation Foundation
{count} votes

Accepted answer
  1. DaisyTian-1203 11,646 Reputation points
    2021-01-15T05:53:51.653+00:00

    ScrollViewer.VerticalOffset is used to get a value that contains the vertical offset of the scrolled content, it has no set method, you can't set a value to it directly , you can refer to ScrollViewer.VerticalOffset Property for more info. You can use ScrollToVerticalOffset to the specified vertical offset position and set IsDeferredScrollingEnabled = true to keep the content is stationary when the user drags the Thumb of a ScrollBar.

    Please add SizeChanged event and Loaded event to your DataGrid, and implment below code in cs:

    ScrollViewer sv1;  
            private void dataGrid_SizeChanged(object sender, SizeChangedEventArgs e)  
            {  
                sv1 = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(this.dataGrid, 0), 0) as ScrollViewer;  
                sv1.IsDeferredScrollingEnabled = true;  
                sv1.ScrollToVerticalOffset(123);  
            }  
      
            private void dataGrid_Loaded(object sender, RoutedEventArgs e)  
            {  
                sv1 = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(this.dataGrid, 0), 0) as ScrollViewer;  
                sv1.IsDeferredScrollingEnabled = true;  
                sv1.ScrollToVerticalOffset(123);  
            }  
    

    By the way, if I misunderstand your question, please point out.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.