Skip to content

Fix VirtualizingStackPanel and nth-child for the currently realizing item container #12957

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

Merged
merged 2 commits into from
Sep 21, 2023

Conversation

MrJul
Copy link
Member

@MrJul MrJul commented Sep 20, 2023

What does the pull request do?

This PR ensures that VirtualizingStackPanel.ContainerFromIndex/IndexFromContainer handles the container currently being realized, so index-based styles (nth-child and nth-last-child) are correctly applied.

What is the current behavior?

Currently, any style targeting a descendant element of an item container having itself an index-based style activator isn't applied when the item container is created by a VirtualizingStackPanel. See #12838 for details.

What is the updated/expected behavior with this PR?

Styles on item container descendants are correctly applied.

How was the solution implemented (if it's not obvious)?

The container currently being realized is stored in a field, so ContainerFromIndex and IndexFromContainer can return it correctly. Before the PR, these methods looked only at the already realized items, but the currently realizing item isn't in that list yet, so NthChildActivator fails on it.

Why did this work previously for container styles (ListBoxItem:nth-child(even)) but not for styles targeting descendants inside the item template (ListBoxItem:nth-child(even) > TextBlock)?

Well, it didn't at first: a matching NthChildActivator wasn't activated during the container creation (CreateContainer), similar to this issue. However, after creation the panel raises ChildIndexChanged (in ItemContainerPrepared), handled by the activator which now gets a proper index.

Elements from the item template aren't created at this point though, as this only happens when the container is measured, which is after ItemContainerPrepared (so the ChildIndexChanged event was already raised and handled).

While this PR adds a special case for the container being realized, ideally we should handle the indexes properly for all containers that have just been realized but aren't part of the _realizedElements yet (they're in _measureElements temporarily during RealizeElements()), as their index might be requested by some code. Pinging @grokys for thoughts on this.

Unit tests have been added.

Fixed issues

@avaloniaui-team
Copy link
Contributor

You can test this PR using the following package version. 11.0.999-cibuild0039704-beta. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@MrJul MrJul force-pushed the fix/vstackpanel-nthchild-template branch from a0663a8 to 5c91ef8 Compare September 20, 2023 13:56
@avaloniaui-team
Copy link
Contributor

You can test this PR using the following package version. 11.0.999-cibuild0039708-beta. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@maxkatz6 maxkatz6 added this pull request to the merge queue Sep 20, 2023
Merged via the queue into AvaloniaUI:master with commit 3515b99 Sep 21, 2023
@MrJul MrJul deleted the fix/vstackpanel-nthchild-template branch September 21, 2023 10:30
@MrJul MrJul added the backport-candidate-11.0.x Consider this PR for backporting to 11.0 branch label Sep 30, 2023
grokys pushed a commit that referenced this pull request Oct 2, 2023
…item container (#12957)

* Added failing tests for VirtualizingStackPanel and nth-child

* VirtualizingStackPanel: support index of currently realizing item
@grokys grokys added backported-11.0.x and removed backport-candidate-11.0.x Consider this PR for backporting to 11.0 branch labels Oct 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nth-child can't target ListBox content (children of ListBoxItem)
4 participants