I have added a Google font to my application using the method described here. It's working great!
In the application, users can choose which font to use in some UI elements. This includes system fonts, and now I'd like to add the Google font to this list as well. However I'm struggling to add, or reference, a friendly looking name for this font rather than the entire resource URI pointing to the font itself.
Here is how I currently have the FontFamily resource defined in App.xaml, based on the previously linked StackOverflow answer:
<FontFamily x:Key="Montserrat">pack://application:,,,/Themes/General/Fonts/#Montserrat</FontFamily>
I have a ComboBox defined like so:
<ComboBox x:Name="PART_editor"
Tag="{Binding}"
SelectedValue="{Binding Value}"
IsEnabled="{Binding IsEditable}"
ItemsSource="{Binding Source={x:Static t:MyFonts.MyFontFamilies}, Converter={StaticResource SystemFontFamiliesConverter}}"
VerticalAlignment="Stretch">
The collection it's bound to is like so:
public static class MyFonts
{
private static Collection<FontFamily> myFontFamilies = null;
public static Collection<FontFamily> MyFontFamilies
{
get
{
if (myFontFamilies == null)
{
myFontFamilies = new(Fonts.SystemFontFamilies.ToList());
myFontFamilies.Add(Application.Current.FindResource("Montserrat") as FontFamily);
}
return myFontFamilies;
}
}
}
The converter on the ComboBox is just used to sort FontFamily names, so I'll leave it out unless someone thinks it's relevant.
When the ComboBox options are displayed, you get an ugly Uri listed instead of just "Montserrat":
I have tried something similar to the below where you list the FamilyNames in the XAML, but you're unable to add the URI as the XAML value in this case without a syntax editor.
<FontFamily x:Key="Montserrat">
<FontFamily.FamilyNames>
<System:String x:Key="en-US">Montserrat</System:String>
</FontFamily.FamilyNames>
!!! syntax error
pack://application:,,,/Themes/General/Fonts/#Montserrat
</FontFamily>
So, what's the best way to get this font's friendly name to show in my combobox?