一个简单的WPF字体选择器实现

原文: 一个简单的WPF字体选择器实现

很久没有写博客了。

这是放暑假中的第一篇博客,以后会多多更新!!!

这就是我写的一个字体选择器,界面如下:

image

本程序用到的技术比较简单,仅仅是用了Font类的几个方法和数据绑定而已。

首先建一个四行两列的Grid,添加一个ComboBox,命名为fonts。然后在添加若干个TextBlock和一个TextBox(用于显示字体名称),如上图所示。

以下是完整的xaml:

<Window x:Class="WpfApplication7.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="字体选择器" Height="350" Width="594" Loaded="MainWindow_Loaded_1"> 
    <Grid> 
        <Grid.RowDefinitions> 
            <RowDefinition Height="2*"></RowDefinition> 
            <RowDefinition Height="2*"></RowDefinition> 
            <RowDefinition Height="2*"></RowDefinition> 
            <RowDefinition Height="4*"></RowDefinition> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
            <ColumnDefinition></ColumnDefinition> 
            <ColumnDefinition></ColumnDefinition> 
        </Grid.ColumnDefinitions> 
        <ComboBox HorizontalAlignment="Center" MinWidth="200" Name="fonts" SelectionChanged="fonts_SelectionChanged" Grid.ColumnSpan="2" MinHeight="30" Margin="20"></ComboBox> 
        <TextBlock Grid.Row="1">中文预览:</TextBlock> 
        <TextBlock Grid.Row="2" Name="text" FontSize="30">你选择的字体</TextBlock> 
        <TextBlock Grid.Row="1" Grid.Column="2">英文预览:</TextBlock> 
        <TextBlock Grid.Row="2" Grid.Column="2" FontSize="30" FontFamily="{Binding Path=FontFamily,ElementName=text}">You Selected Font</TextBlock> 
        <Border Grid.ColumnSpan="2" Grid.Row="3">        
           <DockPanel> 
             <TextBlock DockPanel.Dock="Top">字体详细信息:</TextBlock>       
                <StackPanel> 
                    <StackPanel Orientation="Horizontal"> 
                        <TextBlock>字体名称:</TextBlock> 
                        <TextBox Text="{Binding ElementName=text,Path=FontFamily}" Name="fontname"></TextBox> 
                    </StackPanel> 
                        
                </StackPanel> 
                <Button HorizontalAlignment="Center" VerticalAlignment="Bottom" DockPanel.Dock="Bottom" Click="Button_Click_1"> 
                   <Button.Style> 
                       <Style> 
                           <Style.Triggers> 
                               <Trigger Property="Button.IsMouseOver" Value="True"> 
                                   <Setter Property="Button.Foreground" Value="Blue"></Setter> 
                               </Trigger> 
                               <Trigger Property="Button.IsMouseOver" Value="False"> 
                                   <Setter Property="Button.Foreground" Value="Black"></Setter> 
                               </Trigger> 
                           </Style.Triggers> 
                       </Style> 
                   </Button.Style> 
                   <Button.Template> 
                       <ControlTemplate> 
                            <TextBlock > 
                                <Underline>关注我的新浪微博</Underline> 
                            </TextBlock>   
                       </ControlTemplate> 
                   </Button.Template> 
               </Button>    
           </DockPanel> 
        </Border> 
    </Grid> 
</Window>

其中用了元素绑定,如  FontFamily="{Binding Path=FontFamily,ElementName=text}">,ElementName代表要绑定的元素名称(Name属性),Path代表要绑定的属性名称。

这里我们用Fonts.SystemFontFamilies集合获取系统内安装的字体,然后使用foreach循环添加字体到名称到ComboBox的Items集合中。

窗口load事件代码:

foreach (FontFamily font in Fonts.SystemFontFamilies)

{

     fonts.Items.Add(font.Source);
/* 何问起 hovertree.com */
}

运行一下,会发现ComboBox中显示出所有字体的名称,但美中不足的是不能在列表中显示出字体的字形,我们可以用自定义控件解决。

建一个WPF控件(取名Items),里面放一个TextBlock控件,命名为text,然后在构造函数中键入以下代码:

public Items(FontFamily font) 

    InitializeComponent(); 
    text.FontFamily = font; 
    text.Text= font.Source; 
   
}

其中增加了一个参数用以接受FontFamily对象,设置TextBlock的内容和FontFamily为传递来的参数。

代码如下:

foreach (FontFamily font in Fonts.SystemFontFamilies)  //遍历字体集合中德字体 
{ 
    /* 何问起 hovertree.com */
    fonts.Items.Add(new Items(font));//将参数传递到自定义控件 
}

这样就OK了。

推荐:http://www.cnblogs.com/roucheng/category/827769.html

猜你喜欢

转载自www.cnblogs.com/lonelyxmas/p/10716873.html