<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”?
你是想要这样的吧
<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); } } } } } }
对,就是这种,太感谢啦!!
@Bat-Man:
如果还想要英文的就加个else
else { string _fontName = null; if (_fontDic.TryGetValue(XmlLanguage.GetLanguage("en-us"), out _fontName)) { cbo_Demo.Items.Add(_fontName); } }