0
votes

The project I am working on has a rich text box that can have it's font changed to any of the system fonts via a combo box.

We add all the FontFamily objects from "Fonts.SystemFontFamilies" as the ItemsSource of the combo box. We need to be able to show the localized names of these fonts if they exist within each FontFamily.

I'm currently assuming that the text we see displayed for each ComboBoxItem is the Source property of the FontFamily objects we set as the ItemsSource. Is there a simple way to localize this source? I have been able to get the localized names using:

font.FamilyNames.TryGetValue(XmlLanguage.GetLanguage("ja-JP"), out name);

but as FontFamily.Source is readonly, I am unable to assign this new name to the FontFamily objects.

-- UPDATE --

We have decided not to use a class that contains a font family due to complications with integrating it with the application. I want to try using a value converter to avoid having to change the underlying code.

I have the following value converter class:

class FontValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        FontFamily font = (FontFamily)value;
        string localizedName = font.FamilyNames[XmlLanguage.GetLanguage(culture.Name)];

        if (!String.IsNullOrEmpty(localizedName))
            return localizedName;

        return font.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException("ConvertBack not supported");
    }
}

I am trying to bind this to the ComboBoxItem objects. We set the ItemsSource in the code behind file; basically "ItemsSource = collectionOfFonts". What I want to do is bind my converter to each item in ItemsSource so that it will converter to the localized font family name.

I have been considering the following XAML:

         <Style
            x:Key="ComboBoxItemStyle"
            TargetType="{x:Type ComboBoxItem}">
            <Setter
                Property="Template">
                <Setter.Value>
                    <ControlTemplate
                        TargetType="{x:Type ComboBoxItem}">
                        <ContentPresenter
                            Content="{Binding Source=..., Converter={StaticResource FontValueConverter}}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

However, I am confused as to how I can bind so it retrieves a FontFamily object and converts it to a string to return as the value to be displayed.

Any thoughts?

3

3 Answers

1
votes

We discovered the simplest solution that requires minimal changes to the rest of the application was to instantiate a new FontFamily object using the localized name of the font if available. This was done by creating the following method which returns a new FontFamily object if necessary:

    private FontFamily GetLocalizedFontFamily(FontFamily font)
    {
        string localizedName = font.FamilyNames[XmlLanguage.GetLanguage(CultureInfo.CurrentUICulture.Name)];
        if (!String.IsNullOrEmpty(localizedName))
            return new FontFamily(localizedName);

        return font;
    }

Using this approach meant that we only had to alter the code where the items are added to the combo box and required absolutely no changes elsewhere.

Example of use:

foreach (FontFamily font in Fonts.SystemFontFamilies)
{
    FontFamily localizedFont = GetLocalizedFontFamily(font);
    FontFamilyComboBox.Items.Add(localizedFont);
}
0
votes

Consider a custom Value Converter that leverages the code you have shown, this should allow you to easily bind to the font families enum while providing localized names where available. The TypeConverter should only be used for Content binding, e.g. you don't want to translate the FontFamily itself in the combo box, you want to instead use it within an item/data template for the text display only.

Alternatively, you can generate a container object that exposes a "DisplayName" property in addition to a "FontFamily" property, where DisplayName is the localized result you desire. Bindings against the SelectedItem (or similar) would have to include the "FontFamily" property in the binding path to continue functioning.

Hope that helps.

0
votes

Our eventual solution was to create a new class called LocalizedFontFamily. Since FontFamily is sealed we can't extend it so we passed it in as a parameter to the LocalizedFontFamily classes constructor.

We gave this class a ToString method that would get the localized font name and return it if possible:

    public override string ToString()
    {
        string localizedName = fontFamily.FamilyNames[XmlLanguage.GetLanguage(CultureInfo.CurrentUICulture.Name)];

        if (!String.IsNullOrEmpty(localizedName))
            return localizedName;

        return fontFamily.ToString();
    }