0

I am having the Scrollview as following. Now what my scenario is, Initially the scrollviewr will display 10 items. When the scrollbar reaches the 10th item I need some functionality to be implemented. Now the problem is I am unable to to recognize when the scrollbar is reaching the last item. I tried to use the ScrollChanged event with the vertical offsets but that seems to be not working. Is there any way to achieve this.

                        <ScrollViewer x:Name="ChatListScrollViewer" Background="#ffffff" Grid.Column="0" ScrollChanged="ChatListScrolled" Focusable="False">
                        <VirtualizingStackPanel VerticalAlignment="Stretch">
                            <ItemsControl VirtualizingStackPanel.VirtualizationMode="Recycling" Background="White" x:Name="MessageList"  ItemsSource="{Binding Source={StaticResource ChatList}}" Focusable="False" BorderBrush="Transparent" BorderThickness="0" HorizontalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Hidden" x:FieldModifier="public">
                                <ItemsControl.GroupStyle>
                                    <GroupStyle>
                                        <GroupStyle.HeaderTemplate>
                                            <DataTemplate>
                                                <!-- Code -->
                                            </DataTemplate>
                                        </GroupStyle.HeaderTemplate>
                                    </GroupStyle>
                                </ItemsControl.GroupStyle>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel  Margin="0">
                                            <!-- Code -->
                                        </StackPanel>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </VirtualizingStackPanel>
                    </ScrollViewer>

EDIT: The scrolling is from bottom to up for my scenario. This is How to find that ScrollViewer is scrolled to the end in WPF? useful to detect the last item of the when the scrolling is downwards. In my scenario, its a like chat application. When the scrollbar is scrolled towards up, and reaches the last messages I need to have retrieve some more data .

sahithi
  • 1,039
  • 2
  • 14
  • 37
  • 1
    Does this answer your question? [How to find that ScrollViewer is scrolled to the end in WPF?](https://stackoverflow.com/questions/10793717/how-to-find-that-scrollviewer-is-scrolled-to-the-end-in-wpf) – Nawed Nabi Zada Jun 08 '20 at 08:28
  • Thanks for the response @NawedNabiZada, I tried that solution but that is not working for me. – sahithi Jun 08 '20 at 08:33
  • Perhaps edit your question and state what is not working ? – Nawed Nabi Zada Jun 08 '20 at 08:34
  • With the solution you suggested, the condition at scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight failed. When I reached the last item of the scrollviewer the VerticalOffset sets to 48 but ScrollableHeight is 890.000. So, the condition failed. – sahithi Jun 08 '20 at 09:33
  • If you want to find out if scrollViewer is scrolled to the top then the vertical offset is 0. – Nawed Nabi Zada Jun 08 '20 at 10:04
  • The scrollable bottom is reached when `VerticalOffset` == `ScrollableHeight` or alternatively `VerticalOffset == ExtentHeight - ViewportHeight`. The top scrollable position is `VerticalOffset == 0`. What are you trying to achieve? If you want to implement UI virtualization you should use `ListBox` or `ListView` instead of `ItemsControl`. Both list controls have UI virtualization enabled by default. – BionicCode Jun 09 '20 at 16:19
  • 1
    For data virtualization you should think in pages, where each page contains _n_ items. _n_ should be at least `ScrollViewer.ViewportHeight` or rather `VirtualizingPanel.CacheLength + ScrollViewer.ViewportHeight`. To allow smooth scrolling experience you should preload e.g. next page(s) page before the current page is completely into view. Dummy items prevent the scroll viewer from resizing when adding new items. – BionicCode Jun 09 '20 at 16:29

0 Answers0