[英]Fonts names in the Combobox List XAML c#
I'm designing a list of Fonts, where I refer to Fonts.SystemFontFamilies, but also I want to include 3 custom fonts that exist in my MyDllProject project.我正在设计 Fonts 的列表,其中我指的是 Fonts.SystemFontFamilies,但我还想包括 MyDllProject 项目中存在的 3 个自定义 fonts。 First I add the SystemFontFamilies, and works good, then I add my custom fonts (Roboto, Abstractus and OpenSans) and these add them well to the list, but it gives me the prefix ./# .
首先,我添加了 SystemFontFamilies,并且运行良好,然后我添加了我的自定义 fonts(Roboto、Abstractus 和 OpenSans)并将它们很好地添加到列表中,但它给了我前缀./# 。 So, I would like to find a way to show the names of the font just like Abstractus, Roboto and Open Sans without nothing else.
所以,我想找到一种方法来显示字体的名称,就像 Abstractus、Roboto 和 Open Sans 一样,没有别的。
The code of the comboBox is comboBox的代码是
if( _cmbFontFamilies != null )
{
_cmbFontFamilies.ItemsSource = FontUtilities.Families.OrderBy( fontFamily => fontFamily.Source );
}
and the code for FontUtilities is FontUtilities 的代码是
internal class FontUtilities
{
internal static IEnumerable<FontFamily> Families
{
get
{
foreach ( FontFamily font in Fonts.SystemFontFamilies)
{
yield return font;
}
foreach (FontFamily fontFamily in Fonts.GetFontFamilies(new
Uri("pack://application:,,,/MyDllProject ;Component/Resources/")))
{
yield return fontFamily;
}
}
}
}
Each resulting FontFamily
object returned by Fonts.GetFontFamilies
has a friendly name of the form "./#Family Name"
. Fonts.GetFontFamilies 返回的每个结果
FontFamily
Fonts.GetFontFamilies
都有一个格式为"./#Family Name"
的友好名称。 This friendly name sets a Source
property of FontFamily
and the value of Source
property is returned by FontFamily.ToString()
method.此友好名称设置
FontFamily
的Source
属性, Source
属性的值由FontFamily.ToString()
方法返回。
I would create a custom class FontName
我会创建一个自定义的 class
FontName
public class FontName
{
public string Name { get; set; }
public string Source { get; set; }
}
and return a collection of FontName
by FontUtilities.Families
.并通过
FontUtilities.Families
返回FontName
的集合。 When loading custom fonts remove prefix "./#"
加载自定义 fonts 时删除前缀
"./#"
internal class FontUtilities
{
internal static IEnumerable<FontName> Families
{
get
{
foreach ( FontFamily font in Fonts.SystemFontFamilies)
{
yield return new FontName { Name = font.Source, Source = font.Source };
}
foreach (FontFamily fontFamily in Fonts.GetFontFamilies(new
Uri("pack://application:,,,/MyDllProject ;Component/Resources/")))
{
yield return new FontName { Name = fontFamily.Source.Replace("./#", string.Empty), Source = fontFamily.Source };
}
}
}
}
if( _cmbFontFamilies != null )
{
_cmbFontFamilies.SelectedValuePath = "Source";
_cmbFontFamilies.DisplayValuePath = "Name";
_cmbFontFamilies.ItemsSource = FontUtilities.Families.OrderBy( fontFamily => fontFamily.Source );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.