FontFamily获取中文名字

这毛问题困扰我几天了,主要是关键字不好搜索,不过进过大量的点击搜索,无意中打开一个网页,居然找到了

设置字体,我们都喜欢这样

<Grid>

                    <Grid.Resources>

                        <CollectionViewSource x:Key="sysFonts" Source="{x:Static Member=Fonts.SystemFontFamilies}"></CollectionViewSource>

                    </Grid.Resources>

                    <Grid.ColumnDefinitions>

                        <ColumnDefinition Width="1*"></ColumnDefinition>

                        <ColumnDefinition Width="3*"></ColumnDefinition>

                    </Grid.ColumnDefinitions>

                    <Label Content="选择字体:" Grid.Column="0"></Label>

                    <ComboBox Grid.Column="1" ItemsSource="{Binding Source={StaticResource sysFonts}}" SelectedValue="{Binding FontFamilySelected}"></ComboBox>

                </Grid>

但是,有个问题,这样做ComboBox中是这样的

FontFamily获取中文名字

全是英文的,什么宋体呀,都是英文的,这样不好,那么要怎么样做呢,请看原文地址http://www.oschina.net/code/snippet_67021_837

我做的话,那就在后台做,做一个类型转换器Converet

public class StringToFontFamily : IValueConverter

    {



        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            FontFamily fontfamily = (FontFamily)value;

            LanguageSpecificStringDictionary lsd = fontfamily.FamilyNames;

            if (lsd.ContainsKey(XmlLanguage.GetLanguage("zh-cn")))

            {

                string fontname = null;

                if (lsd.TryGetValue(XmlLanguage.GetLanguage("zh-cn"), out fontname))

                {

                    return fontname;

                }

            }

            else

            {

                string fontname = null;

                if (lsd.TryGetValue(XmlLanguage.GetLanguage("en-us"), out fontname))

                {

                    return fontname;

                }

            }

            return "Arial";

        }



        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            string fontname = (string)value;

            FontFamily fontfamily = new FontFamily(fontname);

            return fontfamily;

        }

    }
private void InitFontFamily()

        {

            foreach (FontFamily fontfamily in Fonts.SystemFontFamilies)

            {

                LanguageSpecificStringDictionary lsd = fontfamily.FamilyNames;

                if (lsd.ContainsKey(XmlLanguage.GetLanguage("zh-cn")))

                {

                    string fontname = null;

                    if (lsd.TryGetValue(XmlLanguage.GetLanguage("zh-cn"), out fontname))

                    {

                        cboFontFamily.Items.Add(fontname);

                    }

                }

                else

                {

                    string fontname = null;

                    if (lsd.TryGetValue(XmlLanguage.GetLanguage("en-us"), out fontname))

                    {

                        cboFontFamily.Items.Add(fontname);

                    }

                }

            }

        }

FontFamily获取中文名字我们看到中文了。。。

 

 

你可能感兴趣的:(font)