Fix #20625: fix compiled binding DataContext inference in ItemTemplate - #21248
Conversation
…temTemplate The Sandbox repro failed with AVLN2100/AVLN2000 when compiled bindings were used with ItemTemplate and name-based DataContext access. Root causes: - No fallback DataContext type was inferred for the root object when x:DataType was absent. - Name-scope lookup could capture stale DataContext metadata from an unrelated traversal branch, causing #ListBoxRoot.DataContext to incorrectly resolve to the item type. Fixes: - Add root-level DataContext fallback inference in AvaloniaXamlIlDataContextTypeTransformer. - Update ScopeRegistrationFinder to resolve DataContext from the current ancestor stack when the matching name is found. - Preserve first-match semantics and prefer root namescope lookup before deferred scopes in name binding resolution. - Add and expand unit tests for ItemTemplate and nested namescope scenarios, including mismatched runtime DataContext behavior. Signed-off-by: João Cruz <joaosantaremdacruz@tecnico.ulisboa.pt>
|
You can test this PR using the following package version. |
|
|
@cla-avalonia agree |
This is not how data contexts work, this change should be completely removed from the PR. |
Address review feedback to reduce test complexity in XamlIlTests. This change removes per-test UserControl helper classes and command scaffolding that were not required to validate the compiler behavior. Tests now use inline XAML in each [Fact] and shared lightweight mock types for root and item data. The compiled binding scenarios are still covered, including root fallback inference and named DataContext resolution with ItemTemplate and nested namescopes, but with less boilerplate and better readability.
|
You can test this PR using the following package version. |
| // starting type for compiled bindings. Falling back to the root CLR type | ||
| // allows expressions to compile and avoids stale inference from unrelated | ||
| // branches, while runtime DataContext semantics remain unchanged. | ||
| inferredDataContextTypeNode = new AvaloniaXamlIlDataContextTypeMetadataNode(on, on.Type.GetClrType()); |
There was a problem hiding this comment.
As mentioned, this change isn't related to the issue and isn't necessary. We do not want automatic inference to fall back to the root type.
Address review feedback by removing the automatic fallback inference to the root type in AvaloniaXamlIlDataContextTypeTransformer, as it is not necessary to fix the issue and is not desired behavior. Also removes the unit tests that validated this specific fallback.
|
You can test this PR using the following package version. |
|
You can test this PR using the following package version. |
…temTemplate (AvaloniaUI#21248) * Fix AvaloniaUI#20625: fix compiled binding DataContext inference in ItemTemplate The Sandbox repro failed with AVLN2100/AVLN2000 when compiled bindings were used with ItemTemplate and name-based DataContext access. Root causes: - No fallback DataContext type was inferred for the root object when x:DataType was absent. - Name-scope lookup could capture stale DataContext metadata from an unrelated traversal branch, causing #ListBoxRoot.DataContext to incorrectly resolve to the item type. Fixes: - Add root-level DataContext fallback inference in AvaloniaXamlIlDataContextTypeTransformer. - Update ScopeRegistrationFinder to resolve DataContext from the current ancestor stack when the matching name is found. - Preserve first-match semantics and prefer root namescope lookup before deferred scopes in name binding resolution. - Add and expand unit tests for ItemTemplate and nested namescope scenarios, including mismatched runtime DataContext behavior. Signed-off-by: João Cruz <joaosantaremdacruz@tecnico.ulisboa.pt> * Fix AvaloniaUI#20625: simplify compiled binding regression tests Address review feedback to reduce test complexity in XamlIlTests. This change removes per-test UserControl helper classes and command scaffolding that were not required to validate the compiler behavior. Tests now use inline XAML in each [Fact] and shared lightweight mock types for root and item data. The compiled binding scenarios are still covered, including root fallback inference and named DataContext resolution with ItemTemplate and nested namescopes, but with less boilerplate and better readability. * Fix AvaloniaUI#20625: remove automatic DataContext fallback to root type Address review feedback by removing the automatic fallback inference to the root type in AvaloniaXamlIlDataContextTypeTransformer, as it is not necessary to fix the issue and is not desired behavior. Also removes the unit tests that validated this specific fallback. --------- Signed-off-by: João Cruz <joaosantaremdacruz@tecnico.ulisboa.pt> Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
The Sandbox repro failed with AVLN2100/AVLN2000 when compiled bindings were used with ItemTemplate and name-based DataContext access.
Root causes:
Fixes:
What does the pull request do?
This PR fixes an issue where shorthand DataContext bindings (introduced in Avalonia 11.3.0) fail to resolve correctly when preceded by an ItemTemplate within the same view. It addresses the compiled binding errors (AVLN2100 and AVLN2000) by correcting how the XAML compiler infers the data context type during scope registration and name lookup.
What is the current behavior?
Currently, if an ItemTemplate (such as one inside a ComboBox) is declared before an element using a shorthand DataContext binding (e.g., {Binding #ListBoxRoot.DataContext.ButtonCommand} inside a ListBox), the compiler incorrectly captures stale DataContext metadata from the unrelated traversal branch. It evaluates the target element's DataContext as matching the preceding ItemTemplate's context rather than the actual parent, causing the application to crash during compilation.
What is the updated/expected behavior with this PR?
The DataContext now correctly resolves to the parent element it is assigned to. Shorthand bindings placed after other ItemTemplate definitions no longer confuse the compiler's scope lookup, allowing the project to build and the bindings to resolve accurately.
How was the solution implemented (if it's not obvious)?
Added root-level DataContext fallback inference in AvaloniaXamlIlDataContextTypeTransformer to handle cases where x:DataType is absent.
Updated ScopeRegistrationFinder so that when a matching name is found, it resolves the DataContext from the current ancestor stack rather than capturing stale context from previous branches.
Preserved first-match semantics, giving preference to the root namescope lookup over deferred scopes during name binding resolution.
Added and expanded unit tests covering nested namescopes, ItemTemplate scenarios, and mismatched runtime DataContext behavior.
Checklist
Breaking changes
None.
Obsoletions / Deprecations
None.
Fixed issues
Fixes #20625