11using iNKORE . UI . WPF . Modern . Common ;
22using iNKORE . UI . WPF . Modern . Common . IconKeys ;
3+ using System ;
34using System . ComponentModel ;
5+ using System . Diagnostics ;
46using System . Windows ;
57using System . Windows . Controls ;
8+ using System . Windows . Data ;
69using System . Windows . Media ;
710
811namespace iNKORE . UI . WPF . Modern . Controls
@@ -46,7 +49,7 @@ public FontIcon(string glyph, FontFamily fontFamily) : this()
4649 typeof ( FontFamily ) ,
4750 typeof ( FontIcon ) ,
4851 new FrameworkPropertyMetadata (
49- new FontFamily ( SegoeIconsFontFamilyName ) ,
52+ null ,
5053 OnFontFamilyChanged ) ) ;
5154
5255 /// <summary>
@@ -55,21 +58,31 @@ public FontIcon(string glyph, FontFamily fontFamily) : this()
5558 /// <returns>The font used to display the icon glyph.</returns>
5659 [ Bindable ( true ) , Category ( "Appearance" ) ]
5760 [ Localizability ( LocalizationCategory . Font ) ]
58- public FontFamily FontFamily
61+ public FontFamily ? FontFamily
5962 {
60- get { return ( FontFamily ) GetValue ( FontFamilyProperty ) ; }
63+ get { return ( FontFamily ? ) GetValue ( FontFamilyProperty ) ; }
6164 set { SetValue ( FontFamilyProperty , value ) ; }
6265 }
6366
64- private static void OnFontFamilyChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
67+ protected static readonly DependencyPropertyKey ActualFontFamilyPropertyKey =
68+ DependencyProperty . RegisterReadOnly (
69+ nameof ( ActualFontFamily ) ,
70+ typeof ( FontFamily ) ,
71+ typeof ( FontIcon ) ,
72+ new FrameworkPropertyMetadata ( new FontFamily ( SegoeIconsFontFamilyName ) ) ) ;
73+
74+ public static readonly DependencyProperty ActualFontFamilyProperty = ActualFontFamilyPropertyKey . DependencyProperty ;
75+
76+ public FontFamily ActualFontFamily
6577 {
66- var fontIcon = ( FontIcon ) d ;
67- if ( fontIcon . _textBlock != null )
68- {
69- fontIcon . _textBlock . FontFamily = ( FontFamily ) e . NewValue ;
70- }
78+ get { return ( FontFamily ) GetValue ( ActualFontFamilyProperty ) ; }
79+ private set { SetValue ( ActualFontFamilyPropertyKey , value ) ; }
80+ }
7181
72- FontIconSource . UpdateIconData ( fontIcon , false ) ;
82+
83+ private static void OnFontFamilyChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
84+ {
85+ ( d as FontIcon ) ? . UpdateIconData ( ) ;
7386 }
7487
7588 /// <summary>
@@ -177,18 +190,33 @@ private static void OnFontWeightChanged(DependencyObject d, DependencyPropertyCh
177190 nameof ( Glyph ) ,
178191 typeof ( string ) ,
179192 typeof ( FontIcon ) ,
180- new FrameworkPropertyMetadata ( string . Empty , OnGlyphChanged ) ) ;
193+ new FrameworkPropertyMetadata ( null , OnGlyphChanged ) ) ;
181194
182195 /// <summary>
183196 /// Gets or sets the character code that identifies the icon glyph.
184197 /// </summary>
185198 /// <returns>The hexadecimal character code for the icon glyph.</returns>
186- public string Glyph
199+ public string ? Glyph
187200 {
188- get => ( string ) GetValue ( GlyphProperty ) ;
201+ get => ( string ? ) GetValue ( GlyphProperty ) ;
189202 set => SetValue ( GlyphProperty , value ) ;
190203 }
191204
205+ protected static readonly DependencyPropertyKey ActualGlyphPropertyKey =
206+ DependencyProperty . RegisterReadOnly (
207+ nameof ( ActualGlyph ) ,
208+ typeof ( string ) ,
209+ typeof ( FontIcon ) ,
210+ new FrameworkPropertyMetadata ( string . Empty ) ) ;
211+
212+ public static readonly DependencyProperty ActualGlyphProperty = ActualGlyphPropertyKey . DependencyProperty ;
213+
214+ public string ActualGlyph
215+ {
216+ get => ( string ) GetValue ( ActualGlyphProperty ) ;
217+ private set => SetValue ( ActualGlyphPropertyKey , value ) ;
218+ }
219+
192220 /// <summary>
193221 /// Identifies the <see cref="Icon"/> dependency property.
194222 /// </summary>
@@ -197,7 +225,7 @@ public string Glyph
197225 nameof ( Icon ) ,
198226 typeof ( FontIconData ? ) ,
199227 typeof ( FontIcon ) ,
200- new PropertyMetadata ( null , ( d , e ) => FontIconSource . UpdateIconData ( d as IFontIconClass , true ) ) ) ;
228+ new FrameworkPropertyMetadata ( null , FrameworkPropertyMetadataOptions . AffectsRender | FrameworkPropertyMetadataOptions . AffectsMeasure , OnIconChanged ) ) ;
201229
202230 /// <summary>
203231 /// Gets or sets the wrapped icon, which includes <see cref="Glyph"/> and <see cref="FontFamily"/>. You can get these instances from <see cref="iNKORE.UI.WPF.Modern.Common.IconKeys"/> namespace.
@@ -218,9 +246,21 @@ private static void OnGlyphChanged(DependencyObject d, DependencyPropertyChanged
218246 fontIcon . _textBlock . Text = ( string ) e . NewValue ;
219247 }
220248
221- FontIconSource . UpdateIconData ( fontIcon , false ) ;
249+ ( d as FontIcon ) ? . UpdateIconData ( ) ;
250+ }
251+
252+ private static void OnIconChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
253+ {
254+ ( d as FontIcon ) ? . UpdateIconData ( ) ;
255+ }
256+
257+ private void UpdateIconData ( )
258+ {
259+ this . ActualGlyph = this . Glyph ?? this . Icon ? . Glyph ?? null ;
260+ this . ActualFontFamily = this . FontFamily ?? this . Icon ? . FontFamily ?? new FontFamily ( SegoeIconsFontFamilyName ) ;
222261 }
223262
263+
224264 private protected override void InitializeChildren ( )
225265 {
226266 _textBlock = new TextBlock
@@ -229,13 +269,15 @@ private protected override void InitializeChildren()
229269 HorizontalAlignment = HorizontalAlignment . Stretch ,
230270 VerticalAlignment = VerticalAlignment . Center ,
231271 TextAlignment = TextAlignment . Center ,
232- FontFamily = FontFamily ,
233- FontSize = FontSize ,
234- FontStyle = FontStyle ,
235- FontWeight = FontWeight ,
236- Text = Glyph
237272 } ;
238273
274+ // Setup bindings
275+ _textBlock . SetBinding ( TextBlock . FontFamilyProperty , new Binding { Path = new PropertyPath ( nameof ( ActualFontFamily ) ) , Source = this } ) ;
276+ _textBlock . SetBinding ( TextBlock . FontSizeProperty , new Binding { Path = new PropertyPath ( nameof ( FontSize ) ) , Source = this } ) ;
277+ _textBlock . SetBinding ( TextBlock . FontStyleProperty , new Binding { Path = new PropertyPath ( nameof ( FontStyle ) ) , Source = this } ) ;
278+ _textBlock . SetBinding ( TextBlock . FontWeightProperty , new Binding { Path = new PropertyPath ( nameof ( FontWeight ) ) , Source = this } ) ;
279+ _textBlock . SetBinding ( TextBlock . TextProperty , new Binding { Path = new PropertyPath ( nameof ( ActualGlyph ) ) , Source = this } ) ;
280+
239281 if ( ShouldInheritForegroundFromVisualParent )
240282 {
241283 _textBlock . Foreground = VisualParentForeground ;
@@ -287,12 +329,12 @@ protected override IconSource CreateIconSourceCore()
287329 FontFamily = new FontFamily ( SegoeIconsFontFamilyName ) ;
288330 }
289331 iconSource . FontFamily = FontFamily ;
332+ iconSource . Icon = Icon ;
290333
291334 iconSource . FontWeight = FontWeight ;
292335 iconSource . FontStyle = FontStyle ;
293336
294337 return iconSource ;
295-
296338 }
297339 }
298340}
0 commit comments