-
-
Notifications
You must be signed in to change notification settings - Fork 80
fix: IconAndText doesn't update the icon even the value is changed #420
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
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 bug where the IconAndText control's icon doesn't update when the Icon property value changes. The root cause was using AddOwner to share FontIcon's IconProperty, which prevented proper change notifications on IconAndText.
Changes:
- Changed IconProperty from using
AddOwnertoRegisterto create an independent property with proper change tracking - Added manual icon update logic through OnApplyTemplate override and property change callbacks
- Removed unused namespace imports
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| private FontIcon _fontIcon; | ||
|
|
||
| static IconAndText() | ||
| { | ||
| DefaultStyleKeyProperty.OverrideMetadata(typeof(IconAndText), new FrameworkPropertyMetadata(typeof(IconAndText))); | ||
| } | ||
|
|
||
| public override void OnApplyTemplate() | ||
| { | ||
| base.OnApplyTemplate(); | ||
|
|
||
| _fontIcon = GetTemplateChild("Icon") as FontIcon; | ||
| UpdateIconElement(); | ||
| } | ||
|
|
||
| #region Properties | ||
|
|
||
| public static readonly DependencyProperty IconProperty = FontIcon.IconProperty.AddOwner(typeof(IconAndText)); | ||
| public static readonly DependencyProperty IconProperty = | ||
| DependencyProperty.Register( | ||
| nameof(Icon), | ||
| typeof(FontIconData?), | ||
| typeof(IconAndText), | ||
| new FrameworkPropertyMetadata( | ||
| null, | ||
| FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure, | ||
| OnIconChanged | ||
| ) | ||
| ); | ||
|
|
||
| private static void OnIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
| { | ||
| if (d is IconAndText iconAndText) | ||
| { | ||
| iconAndText.UpdateIconElement(); | ||
| } | ||
| } | ||
|
|
||
| private void UpdateIconElement() | ||
| { | ||
| if (_fontIcon != null) | ||
| { | ||
| _fontIcon.Icon = Icon; | ||
| } | ||
| } |
Copilot
AI
Jan 17, 2026
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 manual icon update mechanism (OnApplyTemplate, OnIconChanged callback, and UpdateIconElement) appears redundant with the existing TemplateBinding in IconAndText.xaml line 20. With the properly registered DependencyProperty using FrameworkPropertyMetadata, the TemplateBinding should automatically propagate Icon property changes to the FontIcon element. The manual update code adds complexity and could potentially conflict with the binding system. Consider testing whether the fix works with only the property registration change (removing OnApplyTemplate override, OnIconChanged callback, UpdateIconElement method, and _fontIcon field) to rely solely on TemplateBinding as is the standard WPF pattern.
No description provided.