Fix for VisualStateManager Setter.TargetName failing when ControlTemplate is applied #33208
+117
−2
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.
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