사용자 지정 IValueConverter를 사용하여 ComboBoxItem 텍스트의 FontFamily를 설정할 수 있습니다. 글꼴 모음이 기호 기반 인 경우 글꼴이 적용되지 않습니다.
XAML
<Window x:Class="WpfApp4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converters="clr-namespace:WpfApp4.Views.Converters"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<converters:PrintableFontFamilyConverter x:Key="PrintableFontFamilyConverter" />
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<ComboBox x:Name="ComboBox" ItemsSource="{Binding Source={x:Static Fonts.SystemFontFamilies}}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Source}" FontFamily="{Binding Converter={StaticResource PrintableFontFamilyConverter}}" Height="20"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBox Grid.Row="1" FontFamily="{Binding ElementName=ComboBox, Path=SelectedValue}"></TextBox>
</Grid>
변환기
public class PrintableFontFamilyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var fontFamily = value as FontFamily;
if (fontFamily != null)
{
foreach (var typeface in fontFamily.GetTypefaces())
{
if (typeface.TryGetGlyphTypeface(out var glyphTypeface))
{
if (glyphTypeface.Symbol)
{
return null;
}
}
}
}
return fontFamily;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
스크린 샷
동적으로 무엇을 의미하는지 모르겠지만 알파벳은 char (65)부터 char (100)까지의 범위에 있다고 생각합니다. 그 범위를 벗어난 경우 글꼴 이름의 첫 번째 문자를 되돌려 놓아야합니다. 그것 또는 당신이하고 싶은 다른 것 .. 그냥 아이디어 .. – johnyTee
https://stackoverflow.com/questions/4798370/wpf-how-to-filter-out-non-roman-fonts-from-fonts-systemfontfamilies –
@ johnyTee는 FontFamily를 사용하여 속성을 쿼리하여 "Webdings"라는 단어가 사람이 읽을 수 있고 이상한 기호가 아닌지 확인한 다음 Arial을 사용하여 "Webdings"텍스트를 렌더링합니다 – GilesDMiddleton