這毛問題困擾我幾天了,主要是關鍵字不好搜索,不過進過大量的點擊搜索,無意中打開一個網頁,居然找到了
設置字體,我們都喜歡這樣
<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中是這樣的
全是英文的,什么宋體呀,都是英文的,這樣不好,那么要怎么樣做呢,請看原文地址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); } } } }
我們看到中文了。。。