Repository navigation
Conversation
|
You can test this PR using the following package version. |
|
You can test this PR using the following package version. |
There was a problem hiding this comment.
🟡 Changes recommended
The updated container-query base can retain a stale cached container in some detach/provider-clear scenarios, which can block re-initialization and should be corrected before approval.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR fixes container queries (WidthQuery/HeightQuery and query combinators) so they can target non-Visual StyledElements by resolving the first Visual in the element’s logical ancestor chain and using that as the starting point for container lookup, while keeping the existing behavior for Visual targets.
Changes:
- Updated container-query activators and query evaluation to accept
StyledElement(not onlyVisual) and to resolve aVisualvia logical ancestors when needed. - Updated AND/OR query evaluation and activator builders to operate on
StyledElement. - Added unit tests covering non-visual targets, logical reparenting scenarios, and ensuring a container doesn’t query itself.
File summaries
| File | Description |
|---|---|
| tests/Avalonia.Base.UnitTests/Styling/ContainerTests.cs | Adds coverage for non-visual container-query targets, reparenting tracking, and “container doesn’t query itself”. |
| src/Avalonia.Base/Styling/ScreenQueries.cs | Removes Visual-only restriction for width/height queries and routes evaluation through StyledElement-based container lookup. |
| src/Avalonia.Base/Styling/OrQuery.cs | Allows OR query evaluation for non-visual styled elements by removing Visual gating. |
| src/Avalonia.Base/Styling/AndQuery.cs | Allows AND query evaluation for non-visual styled elements by removing Visual gating. |
| src/Avalonia.Base/Styling/Activators/ScreenActivator.cs | Updates width/height activators to accept StyledElement targets. |
| src/Avalonia.Base/Styling/Activators/OrQueryActivatorBuilder.cs | Switches builder from Visual to StyledElement to support non-visual targets. |
| src/Avalonia.Base/Styling/Activators/OrQueryActivator.cs | Updates constructor to accept StyledElement and pass it to the shared container-query base. |
| src/Avalonia.Base/Styling/Activators/ContainerQueryActivatorBase.cs | Core change: resolves a Visual for non-visual targets via logical ancestors, tracks logical attach/detach, and adjusts container lookup accordingly. |
| src/Avalonia.Base/Styling/Activators/AndQueryActivatorBuilder.cs | Switches builder from Visual to StyledElement to support non-visual targets. |
| src/Avalonia.Base/Styling/Activators/AndQueryActivator.cs | Updates constructor to accept StyledElement and pass it to the shared container-query base. |
Review details
- Files reviewed: 10/10 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
|
||
| if (_visual is { } oldVisual) | ||
| { | ||
| oldVisual.AttachedToVisualTree -= Visual_AttachedToVisualTree; | ||
| oldVisual.DetachedFromVisualTree -= Visual_DetachedFromVisualTree; |
There was a problem hiding this comment.
I am not sure if this is relevant to this PR. As far as I can see, the part
if (_currentScreenSizeProvider is { } && Container.GetQueryProvider(_currentScreenSizeProvider) is { } provider)
{
provider.WidthChanged -= WidthChanged;
provider.HeightChanged -= HeightChanged;
_currentScreenSizeProvider = null;
}predates this PR and was previously called by Deinitialize. But I can move the field outside as well, if thats desired.
NuGet packagesThe packages for this pull request have been published (workflow run):
|
|
Sorry for the late reply. Could you add a sample demonstrating the added behavior? |
|
Hi @emmauss, no worries. I added an example to the I was not sure where you wanted it. we could also add it to the TableView page directly (because this is the main contender for a non-visual |
|
It would be best for this specific one to be in the TableView sample |
Demonstrates a container query that targets a non-visual StyledElement, as requested in the PR review. The option row wraps so that it does not clip while the page is narrowed.
811efed to
604b21d
Compare
* fix: Support StylableElement for styles inside ContainerQueries * chore: Add container query sample for TableView columns Demonstrates a container query that targets a non-visual StyledElement, as requested in the PR review. The option row wraps so that it does not clip while the page is narrowed.



What does the pull request do?
StyleSelectors work on stylable elements. But for container queries, the query selection silently limits this to visuals only (see #22166 for details).
This PR removes that restriction. For a non-visual
StyledElement, container queries find the firstVisualin its logical ancestors and use that visual as the starting point when searching for a matching container.The existing behavior for visual targets remains unchanged.
What is the current behavior?
Only classes extending
Visualwork; when extending only ' StyledElement', styles are silently ignored.What is the updated/expected behavior with this PR?
Works
How was the solution implemented (if it's not obvious)?
Container-query activators now accept a
StyledElementinstead of requiring aVisual.For a visual target, container lookup still starts with its visual parent.
For a non-visual target, the activator:
Visualin the target's logical ancestors;Checklist
Breaking changes
Obsoletions / Deprecations
Fixed issues
Fixes #22166