resolving-icon-font-inheritance
WPF CustomControl Icon Font Inheritance Issue Resolution
Problem Scenario
When using Segoe Fluent Icons font in WPF CustomControl, TextBlocks within the same ControlTemplate inherit the icon font, causing text to render incorrectly.
Symptoms
- Button text displays as square boxes (□) or strange symbols
- Icons display correctly but regular text doesn't render
Cause
WPF's FontFamily is inherited to child elements following the Visual Tree. When FontFamily="Segoe Fluent Icons" is set on a TextBlock for icons within a ControlTemplate, other TextBlocks in the same container may inherit this font.
Solution
Explicitly Specify FontFamily on Text-Displaying Elements
<!-- IconButton ControlTemplate Example -->
<ControlTemplate TargetType="{x:Type local:IconButton}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel Orientation="{TemplateBinding Orientation}"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<!-- Icon: Use Segoe Fluent Icons -->
<TextBlock x:Name="PART_Icon"
Text="{TemplateBinding Icon}"
FontFamily="Segoe Fluent Icons, Segoe MDL2 Assets"
FontSize="{TemplateBinding IconSize}"
Foreground="{TemplateBinding Foreground}" />
<!-- Text: Explicitly specify Segoe UI (Important!) -->
<TextBlock x:Name="PART_Text"
Text="{TemplateBinding Text}"
FontFamily="Segoe UI"
FontSize="12"
Foreground="{TemplateBinding Foreground}"
VerticalAlignment="Center" />
</StackPanel>
</Border>
</ControlTemplate>
Key Points
- Apply icon font only to specific element: Specify
Segoe Fluent Iconsonly onPART_Icon - Explicitly specify FontFamily on text elements:
FontFamily="Segoe UI"is required onPART_Text - Don't set FontFamily on parent containers: Setting FontFamily on Border or StackPanel inherits to all children
Applicable Controls
- IconButton (icon + text button)
- IconToggleButton (toggleable icon button)
- NavigationButton (navigation menu item)
- Any other CustomControl using icons and text together
Related Icon Fonts
| Font Name | Windows Version | Purpose |
|---|---|---|
Segoe Fluent Icons |
Windows 11+ | Latest Fluent Design icons |
Segoe MDL2 Assets |
Windows 10+ | Default system icons |
Fallback Pattern
FontFamily="Segoe Fluent Icons, Segoe MDL2 Assets"
Specify fallback font for Windows 10 compatibility
More from christian289/dotnet-with-claudecode
converting-html-css-to-wpf-xaml
Converts HTML/CSS to WPF CustomControl XAML with correct patterns and common pitfall solutions. Use when transforming web designs to WPF, converting CSS animations to Storyboards, implementing CSS border-radius clipping, CSS pseudo-elements (::before/::after), or CSS transforms in XAML.
56publishing-wpf-apps
Guides WPF application publishing and installer options. Use when user mentions publish, deploy, release, packaging, or installer to help choose deployment method and installer technology.
14using-avalonia-collectionview
Provides CollectionView alternatives for AvaloniaUI using DataGridCollectionView and ReactiveUI. Use when filtering, sorting, or grouping collections in AvaloniaUI applications.
9designing-avalonia-customcontrol-architecture
Defines the basic solution structure for AvaloniaUI Desktop Applications using CustomControl. Use when creating new AvaloniaUI projects or designing stand-alone control styles with ControlTheme.
9using-xaml-property-element-syntax
Converts long inline XAML bindings to Property Element Syntax for better readability. Use when XAML binding expressions become too long or complex.
8managing-styles-resourcedictionary
Manages WPF Style definitions and ResourceDictionary patterns including BasedOn inheritance and resource merging. Use when building theme systems, organizing resources, or choosing between StaticResource and DynamicResource.
8