AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.95k stars 2.25k forks source link

ScrollIntoView does not work as expected. #17347

Closed NeverMorewd closed 3 weeks ago

NeverMorewd commented 3 weeks ago

Describe the bug

ScrollIntoView(itemsControl.Items.Count - 1) can not work as expected, while scrollViewer?.ScrollToEnd(); can work well.

To Reproduce

  // not work
  itemsControl.ScrollIntoView(itemsControl.Items.Count - 1);
  itemsControl.ItemsView.CollectionChanged += (sender, args) =>
  {
      itemsControl.ScrollIntoView(itemsControl.Items.Count - 1);
  };
  itemsControl.Items.CollectionChanged += (s,e)=> 
  {
      itemsControl.ScrollIntoView(itemsControl.Items.Count - 1);
  };
  itemsControl.Items.Add(viewName);
  // work
  var scrollViewer = itemsControl.FindLogicalAncestorOfType<ScrollViewer>(includeSelf: false);
  scrollViewer?.ScrollToEnd();

Expected behavior

ScrollIntoView(itemsControl.Items.Count - 1) can work like scrollViewer?.ScrollToEnd();

Avalonia version

11.1.3

OS

Windows

Additional context

No response

timunie commented 3 weeks ago

You may need to use the Dispatcher in order to wait for items to be loaded.

NeverMorewd commented 3 weeks ago

You may need to use the Dispatcher in order to wait for items to be loaded.

Thank you!

I have solved this issue with adding Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded); after Items.Add