Fix VirtualizingStackPanel and nth-child for the currently realizing item container #12957
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
andnth-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
andIndexFromContainer
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, soNthChildActivator
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 raisesChildIndexChanged
(inItemContainerPrepared
), 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 theChildIndexChanged
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 duringRealizeElements()
), as their index might be requested by some code. Pinging @grokys for thoughts on this.Unit tests have been added.
Fixed issues