WPF字体网格查看器

时间:2014-05-15 15:28:13

标签: wpf

我有一个用icomoon制作的ttf格式的字形字体,我希望像Windows字体查看器一样显示在网格中,所以每个字形显示在一个单元格中

最好的方法是什么?

感谢和美好的一天

1 个答案:

答案 0 :(得分:1)

也许这个小小的演示可以帮助你入门:

MainWindow.xaml:

<Window x:Class="FontViewer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Font Viewer" Height="350" Width="640">
    <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Visible">
            <ItemsControl ItemsSource="{Binding Glyphs}"  FontFamily="{Binding FontName}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Width="600"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Border Width="30" Height="30" BorderBrush="Black" BorderThickness="1">
                            <TextBlock Text="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
    </Grid>
</Window>

MainWindow.xaml.cs:

using System.Linq;
using System.Windows;
using System.Windows.Media;

namespace FontViewer
{
    public partial class MainWindow : Window
    {
        private char[] _glyphs;
        private FontFamily _fontFamily;

        public MainWindow()
        {
            DataContext = this;

            GlyphTypeface glyphTypeface;
            _fontFamily = new FontFamily(FontName);
            _fontFamily.GetTypefaces().First().TryGetGlyphTypeface(out glyphTypeface);
            _glyphs = glyphTypeface.CharacterToGlyphMap.Select(g => (char)g.Key).ToArray();

            InitializeComponent();
        }

        public char[] Glyphs
        {
            get
            {
                return _glyphs;
            }
        }

        public string FontName
        {
            get
            {
                return "Comic Sans MS";
            }
        }
    }
}

结果如下:

Screenshot