fix: IconAndText doesn't update the icon even the value is changed#420
fix: IconAndText doesn't update the icon even the value is changed#420LxHTT wants to merge 1 commit intoiNKORE-NET:mainfrom
Conversation
There was a problem hiding this comment.
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; | ||
| } | ||
| } |
There was a problem hiding this comment.
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.