Avalonia icon indicating copy to clipboard operation
Avalonia copied to clipboard

Scrolling a ListBox with virtualization messes up the keyboard focus

Open megazyz opened this issue 3 years ago • 9 comments

Describe the bug Scrolling a ListBox with VirtualizationMode=Simple changes the keyboard focus to another item when the focused item gets out of view. The issue does not occur when VirtualizationMode is None.

To Reproduce Steps to reproduce the behavior:

  1. Create a new project in Visual Studio using the Avalonia MVVM Application template.
  2. Add an Items property to MainWindowViewModel: public List<int> Items { get; set; } = new(Enumerable.Range(0, 1000));
  3. Replace the greeting TextBlock to a ListBox in MainWindow.axaml: <ListBox Items="{Binding Items}" VirtualizationMode="Simple"/>
  4. Run the application
  5. Click on an item in the ListBox
  6. Press Up Arrow or Down Arrow keys to make the keyboard focus adorner visible
  7. Start scrolling down using the scrollbar or the mouse wheel (not the keyboard), and keep scrolling
  8. Every time the focused item gets out of the viewport, a new item receives the keyboard focus.
  9. Change VirtualizationMode to None to make the issue disappear

The issue is also visible in the VirtualizationDemo sample.

Expected behavior The keyboard focus should not change while scrolling. The virtualization code should not recycle the element having the keyboard focus.

Desktop (please complete the following information):

  • OS: Windows 10
  • Version: 0.10.18, 11.0.0-preview2

megazyz avatar Oct 23 '22 23:10 megazyz

I'm going to create a pull request to fix this

megazyz avatar Oct 25 '22 16:10 megazyz

Sorry, I got lost, we need an expert here.

megazyz avatar Oct 25 '22 22:10 megazyz

The keyboard focus should not change while scrolling. The virtualization code should not recycle the element having the keyboard focus.

Not sure how possible it is. I wonder, if it was solved in WPF or UWP. Can somebody check?

maxkatz6 avatar Oct 26 '22 01:10 maxkatz6

Also, if ItemsRepeater and DataGrid have the same problem (probably have)

maxkatz6 avatar Oct 26 '22 01:10 maxkatz6

It works in WPF.

megazyz avatar Oct 26 '22 01:10 megazyz

I know it works in WinUI, if you have one of the WinUI control galleries, it's easy to repro.

If you inspect the Native View of Selector in UWP, you can see they have several fields specifically for holding the current focus information for the items: image

And the ViewManager for ItemsRepeater holds focus info too, although I haven't tested that it works: https://github.com/AvaloniaUI/Avalonia/blob/34d24a0f398c2ade43895004c38694b4613779b2/src/Avalonia.Controls/Repeater/ViewManager.cs#L25

amwx avatar Oct 26 '22 02:10 amwx

Checking the visual children of a ListBox using the debugger shows that WPF keeps the focused element alive in the visual tree, and does not reuse it for displaying other items.

megazyz avatar Oct 26 '22 02:10 megazyz

I cannot reproduce this for ItemsRepeater and DataGrid.

megazyz avatar Oct 27 '22 18:10 megazyz

Using ItemsRepeater in ListBox may be a possible solution, see #2594

megazyz avatar Oct 28 '22 00:10 megazyz