Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrolling a ListBox with virtualization messes up the keyboard focus #9269

Closed
megazyz opened this issue Oct 23, 2022 · 9 comments · Fixed by #9677
Closed

Scrolling a ListBox with virtualization messes up the keyboard focus #9269

megazyz opened this issue Oct 23, 2022 · 9 comments · Fixed by #9677
Labels

Comments

@megazyz
Copy link
Contributor

megazyz commented Oct 23, 2022

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 megazyz added the bug label Oct 23, 2022
@megazyz
Copy link
Contributor Author

megazyz commented Oct 25, 2022

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

@megazyz
Copy link
Contributor Author

megazyz commented Oct 25, 2022

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

@maxkatz6
Copy link
Member

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
Copy link
Member

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

@megazyz
Copy link
Contributor Author

megazyz commented Oct 26, 2022

It works in WPF.

@amwx
Copy link
Contributor

amwx commented Oct 26, 2022

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:

private IControl? _lastFocusedElement;

@megazyz
Copy link
Contributor Author

megazyz commented Oct 26, 2022

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
Copy link
Contributor Author

megazyz commented Oct 27, 2022

I cannot reproduce this for ItemsRepeater and DataGrid.

@megazyz
Copy link
Contributor Author

megazyz commented Oct 28, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants