-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix for VisualStateManager Setter.TargetName failing when ControlTemplate is applied #33208
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
base: main
Are you sure you want to change the base?
Conversation
…lution across ControlTemplate namescope boundaries
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a crash in VisualStateManager when using Setter.TargetName with a ControlTemplate. The root cause is that FindByName() only resolves elements within the same namescope, and ControlTemplates create new namescopes. The fix adds a FindTargetByName() helper method that traverses the parent hierarchy to inspect each parent's namescope when the target is not found in the current scope.
Key Changes
- Added namescope traversal logic to resolve targets across ControlTemplate boundaries
- Created UI tests to validate the fix on all platforms
- Improved reliability of visual state setters with named targets in templated scenarios
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/Controls/src/Core/Setter.cs |
Introduced FindTargetByName() method to walk up parent tree when resolving TargetName, enabling cross-namescope resolution for ControlTemplates |
src/Controls/tests/TestCases.HostApp/Issues/Issue26977.xaml |
Added UI test page demonstrating VisualStateManager with Setter.TargetName inside a ControlTemplate |
src/Controls/tests/TestCases.HostApp/Issues/Issue26977.xaml.cs |
Implemented code-behind with state toggling logic for the test scenario |
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue26977.cs |
Added NUnit Appium test to verify the fix doesn't crash and correctly applies visual state setters |
| @@ -0,0 +1,20 @@ | |||
| namespace Maui.Controls.Sample.Issues; | |||
|
|
|||
| [Issue(IssueTracker.Github, 26977, "Setter.TargetName + ControlTemplate crash", PlatformAffected.Android)] | |||
Copilot
AI
Dec 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PlatformAffected should be set to PlatformAffected.All instead of PlatformAffected.Android. According to the PR description, this fix was tested on Android, Windows, iOS, and Mac. The issue is a VisualStateManager namescope resolution problem that affects all platforms where ControlTemplates create namescope boundaries, not just Android.
| [Issue(IssueTracker.Github, 26977, "Setter.TargetName + ControlTemplate crash", PlatformAffected.Android)] | |
| [Issue(IssueTracker.Github, 26977, "Setter.TargetName + ControlTemplate crash", PlatformAffected.All)] |
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Root Cause:
The issue occurs because VisualStateManager setters with TargetName rely on FindByName(), which only resolves elements within the same namescope. Since ControlTemplates create a new namescope, visual states defined inside a template cannot access elements outside it, resulting in the crash: "Cannot resolve [TargetName] as Setter Target."
Fix Description:
The fix involves using element.FindByName() for targets within the same or child namescopes and, if the target is not found, traversing the parent hierarchy to inspect each parent element’s namescope. This approach enables elements defined within a control template to resolve a TargetName outside their own namescope, ensuring reliable target resolution while maintaining compatibility with existing visual state behavior.
Issues Fixed
Fixes #26977
Tested the behaviour in the following platforms
Output Screenshot
26977-BeforeFix.mov
26977-AfterFix.mov