question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ScrollViewer.ScrollToEnd() seems not working

See original GitHub issue

Describe the bug

ScrollViewer.ScrollToEnd() method is not working, content still remains in the same position.

To Reproduce Steps to reproduce the behavior:

  1. Create a ScrollViewer
  2. Add some vertical contents to activate the vertical scroll bar
  3. Call ScrollViewer.ScrollToEnd()
  4. See the error

Expected behavior

ScrollViewer should go to the end of the content.

Screenshots

ScrollViewer’s style:

<Style Selector="ScrollViewer">
        <Setter Property="Background"
                Value="Transparent" />
        <Setter Property="Template">
            <ControlTemplate>
                <Grid ColumnDefinitions="*,Auto" RowDefinitions="*,Auto">
                    <ScrollContentPresenter Name="PART_ContentPresenter"
                                            Grid.Row="0"
                                            Grid.Column="0"
                                            Grid.RowSpan="2"
                                            Grid.ColumnSpan="2"
                                            Background="{TemplateBinding Background}"
                                            CanHorizontallyScroll="{TemplateBinding CanHorizontallyScroll}"
                                            CanVerticallyScroll="{TemplateBinding CanVerticallyScroll}"
                                            Content="{TemplateBinding Content}"
                                            Extent="{TemplateBinding Extent, Mode=TwoWay}"
                                            Margin="{TemplateBinding Padding}"
                                            Offset="{TemplateBinding Offset, Mode=TwoWay}"
                                            Viewport="{TemplateBinding Viewport, Mode=TwoWay}">
                        <ScrollContentPresenter.GestureRecognizers>
                            <ScrollGestureRecognizer
                                CanHorizontallyScroll="{TemplateBinding CanHorizontallyScroll}"
                                CanVerticallyScroll="{TemplateBinding CanVerticallyScroll}" />
                        </ScrollContentPresenter.GestureRecognizers>
                    </ScrollContentPresenter>
                    <ScrollBar Name="PART_HorizontalScrollBar"
                               AllowAutoHide="{TemplateBinding AllowAutoHide}"
                               Orientation="Horizontal"
                               LargeChange="{Binding LargeChange.Width, RelativeSource={RelativeSource TemplatedParent}}"
                               SmallChange="{Binding SmallChange.Width, RelativeSource={RelativeSource TemplatedParent}}"
                               Maximum="{TemplateBinding HorizontalScrollBarMaximum}"
                               Value="{TemplateBinding HorizontalScrollBarValue, Mode=TwoWay}"
                               ViewportSize="{TemplateBinding HorizontalScrollBarViewportSize}"
                               Visibility="{TemplateBinding HorizontalScrollBarVisibility}"
                               Grid.Row="1"
                               Focusable="False" />
                    <ScrollBar Name="PART_VerticalScrollBar"
                               AllowAutoHide="{TemplateBinding AllowAutoHide}"
                               Orientation="Vertical"
                               LargeChange="{Binding LargeChange.Height, RelativeSource={RelativeSource TemplatedParent}}"
                               SmallChange="{Binding SmallChange.Height, RelativeSource={RelativeSource TemplatedParent}}"
                               Maximum="{TemplateBinding VerticalScrollBarMaximum}"
                               Value="{TemplateBinding VerticalScrollBarValue, Mode=TwoWay}"
                               ViewportSize="{TemplateBinding VerticalScrollBarViewportSize}"
                               Visibility="{TemplateBinding VerticalScrollBarVisibility}"
                               Grid.Column="1"
                               Focusable="False" />
                    <Panel x:Name="PART_ScrollBarsSeparator" Grid.Row="1" Grid.Column="1"
                           Background="{DynamicResource ScrollViewerScrollBarsSeparatorBackground}" />
                </Grid>
            </ControlTemplate>
        </Setter>
    </Style>

Desktop (please complete the following information):

  • OS: Windows 11 22H2
  • Version: Latest Avalonia nightly build

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
grokyscommented, Jan 30, 2023

Without an actual repro it’s difficult to say for sure, but indeed in your case it sounds like you will need a layout pass to complete before being able to scroll to the end. That’s not a bug, it’s simply how it works - the contents of the ItemsControl are updated on layout.

That doesn’t mean that all cases of scrolling to the end require a layout pass however, so I think the PR should be closed.

0reactions
laolarou726commented, Jan 17, 2023

There aren’t an examples of where you’re calling ScrollToEnd here so it’s hard to say what’s going wrong, but I assume you’re calling it in the constructor or something?

In this case, @maxkatz6 is correct that a layout pass would be needed before ScrollToEnd can work.

I think, default API can be changed to async and be called on the dispatcher automatically.

However I disagree with this, there are a lot of cases where you don’t need to do this (the majority in fact).

Basically I have an ItemsControl in the ScrollViewer.

I’m calling ScrollViewer.ScrollToEnd() in the event where the items source of ItemsControl is changed. So it should be in the UI thread. If I only use ScrollToEnd() without Dispatcher priority the content will not refresh. So maybe is a bug or something related to virtualization?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Doing AutoScroll with ScrollViewer.ScrollToEnd() only ...
The cause of the problem is that ScrollToEnd() has nothing to do with autoscroll. This call just scrolls to the end and that...
Read more >
ScrollView.scrollTo method not working · Issue #24531
Here is my solution to scrollTo() issue. In order to programmatically scroll, set scrollOverflowEnabled prop to true . <ScrollView ref={scroller ...
Read more >
PageDown with ScrollViewer
Hi, In our app most of our screens/pages/frames fit within the limits of a maximized window and Vertical scrolling is not required. We...
Read more >
[Solved] ScrollViewer ScrollToEnd Buggy - UI
I'm using it as scroll viewer for the chat portion of my game, to scroll chat messages… however it seems to be buggy....
Read more >
ScrollViewer.ScrollToEnd Method (System.Windows. ...
This method scrolls to both the vertical and horizontal end points of the content. This method does not induce any scrolling behavior if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found