1

In WPF I set a style for my control so it has multiple font families specified:

<Style TargetType="{x:Type MyControl}">
    <Setter Property="FontFamily"
            Value="Helvetica, Arial, Segoe UI" />
</Style>

How can I determine which font is actually got selected and is in use for the control?

g t
  • 7,287
  • 7
  • 50
  • 85

2 Answers2

1

According to this blog post:

The XAML text renderer [...] uses the specified list (family) of fonts on a character by character basis.

So there is no ‘font in use for the control’ as you ask.

To find out which font is used for each character, this response looks a good starting point.

Orace
  • 7,822
  • 30
  • 45
  • You can find my test code related to this question [here](https://github.com/Orace/SO/tree/main/SO_70937142). – Orace Feb 01 '22 at 11:23
0

I ended up just having to parse the FontFamily's Source property:

public static FontFamily GetFirstValid(this FontFamily fontFamily)
{
    return fontFamily?.Source.Split(',')
                     .Select(p => new FontFamily(p))
                     .FirstOrDefault(f => f.GetTypefaces()
                                           .Any(t => t.TryGetGlyphTypeface(out GlyphTypeface glyph)
                                                     && glyph?.CharacterToGlyphMap.Count > 0));
}
g t
  • 7,287
  • 7
  • 50
  • 85