ARTICLE

Apply Font Properties on text in WPF

Posted by Sapna Articles | WPF using VB.NET November 03, 2010
In this article you will learn how to apply different types of font properties to the text in WPF.
 
Reader Level:

This article shows a sample application which produces a dialog box that allows the user to customize values of font properties, for example, FontStyle, FontSize, and FontColor. It also demonstrates techniques for creating a modal dialog box using Windows Presentation Foundation (WPF).

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
    x:Class="WpfApplication1.Window1"
    Title="Font Properties VB Sample">
     <StackPanel Background="LemonChiffon">
        <TextBlock FontWeight="Bold" FontSize="14">FontFamly:</TextBlock>
        <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
           <Button Click="OnClick1" Background="Pink" Margin="3" FontSize="13">Arail Black</Button>
            <Button Click="OnClick2" Background="Pink" Margin="3" FontSize="13">Comic Sans MS</Button>
            <Button Click="OnClick3" Background="Pink" Margin="3" FontSize="13">Forte</Button>
            <Button Click="OnClick4" Background="Pink" Margin="3" FontSize="13">Lucida Calligraphy</Button>
            <Button Click="OnClick5" Background="Pink" Margin="3">Ravie</Button>
        </StackPanel>
 
        <TextBlock FontWeight="Bold" FontSize="14">Foreground Color:</TextBlock>
 
       <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
 
           <Button Click="OnClick11" Foreground="White" Background="Red" Margin="3" FontSize="13">Red</Button>
            <Button Click="OnClick12" Foreground="White" Background="Blue" Margin="3" FontSize="13">Blue</Button>
            <Button Click="OnClick13" Foreground="White" Background="Green" Margin="3" FontSize="13">Green</Button>
            <Button Click="OnClick14" Foreground="White" Background="Purple" Margin="3" FontSize="13">Purple</Button>
            <Button Click="OnClick15" Foreground="White" Background="Orange" Margin="3" FontSize="13">Orange</Button>
        </StackPanel>
 
        <TextBlock FontWeight="Bold" FontSize="14" VerticalAlignment="Center">FontSize:</TextBlock>
        <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
            <RadioButton Click="OnClick6" Margin="3" FontSize="13">8 point</RadioButton>
            <RadioButton Click="OnClick7" Margin="3" FontSize="13">12 point</RadioButton>
            <RadioButton Click="OnClick8" Margin="3" FontSize="13">16 point</RadioButton>
            <RadioButton Click="OnClick9" Margin="3" FontSize="13">20 point</RadioButton>
            <RadioButton Click="OnClick10" Margin="3" FontSize="13">24 point</RadioButton>
        </StackPanel>
 
        <TextBlock FontSize="12" Name="txt1" Margin="0,0,0,2">The FontFamily is set to Arial.</TextBlock>
 
       <TextBlock FontSize="12" Name="txt4" Margin="0,0,0,2">The Foreground color is set to Black.</TextBlock>
        <TextBlock FontSize="12" Name="txt3">The FontSize is set to 12 point.</TextBlock>
        <FlowDocumentReader>
            <FlowDocument Name="txt2" FontFamily="Arial" FontSize="14" Foreground="Black">
                <Paragraph>
                    Welcome to Vbdotnetheaven
               
</Paragraph>
            </FlowDocument>
        </FlowDocumentReader>
    </StackPanel>
</
Window>

Output 

fontproperty1.gif

Here you see the font style is set as Arial Black, font color Red and font size is 20 points. Now, lets do some changes in the output window and have fun.

fontproperty2.gif

I hope you will enjoy this.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor