首页 新闻 赞助 找找看

WPF FontFamily设置字体的问题

0
悬赏园豆:5 [已解决问题] 解决于 2013-02-26 11:09
<Window.Resources>
        <CollectionViewSource x:Key="sysFonts" Source="{x:Static Member=Fonts.SystemFontFamilies}"></CollectionViewSource>
    </Window.Resources>

使用上面方法来获取系统中的字体

然后绑定到combobox上

<ComboBox x:Name="myFF" Width="100" Height="30" ItemsSource="{Binding Source={StaticResource sysFonts}}" SelectionChanged="ComboBox_SelectionChanged"></ComboBox>

但是,发现此法获得的字体显示都是英文的,如图

我要怎么弄,它才能用中文来显示呢?比如说“幼圆”就是显示“幼圆”而不是“YouYuan”?

UncleNull的主页 UncleNull | 初学一级 | 园豆:3
提问于:2013-02-25 11:39
< >
分享
最佳答案
1

你是想要这样的吧

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="win_LoadedEvent">
    <Grid>
        <ComboBox x:Name="cbo_Demo" Margin="0,0,291,287"></ComboBox>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Markup;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public void win_LoadedEvent(object sender, RoutedEventArgs e)
        {
            foreach (FontFamily _f in Fonts.SystemFontFamilies)
            {
                LanguageSpecificStringDictionary _fontDic = _f.FamilyNames;
                if (_fontDic.ContainsKey(XmlLanguage.GetLanguage("zh-cn")))
                {
                    string _fontName = null;
                    if (_fontDic.TryGetValue(XmlLanguage.GetLanguage("zh-cn"), out _fontName))
                    {
                        cbo_Demo.Items.Add(_fontName);
                    }
                }
            }
        }
      
    }
}

 

 

 

 

 

 

 

收获园豆:5
li-peng | 小虾三级 |园豆:954 | 2013-02-26 11:07

对,就是这种,太感谢啦!!

UncleNull | 园豆:3 (初学一级) | 2013-02-26 11:08

@Bat-Man: 

如果还想要英文的就加个else 

else
                {
                    string _fontName = null;

                    if (_fontDic.TryGetValue(XmlLanguage.GetLanguage("en-us"), out _fontName))
                    {
                        cbo_Demo.Items.Add(_fontName);
                    }

                }
li-peng | 园豆:954 (小虾三级) | 2013-02-26 11:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册