WPF ListBox List Fonts


This entry is part 6 of 6 in the series WPF ListBox

Here is another example of the ListBox. Here we will display all of the system fonts.

Below is a screenshot of the program. When the program first runs there are no fonts shown in the list box. The user can change the “Sample Text” to some text of there choice. After that, if they click the button, the list of fonts will appear. The user can also change the font size with the slider.

Below is the XAML source code and after that we have the code behind.

<Window x:Class="ListBoxDisplayFonts.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:local="clr-namespace:ListBoxDisplayFonts"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Height="450" Width="500" MinHeight="400" MinWidth="470"
        Title="ListBoxDisplayFonts - Inspired by: CSharpHelper.com" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
            <ColumnDefinition Width="30"/>
        </Grid.ColumnDefinitions>

        <Label Grid.Row="0" Grid.Column="0" Content="Text:" Margin="5"/>
        <TextBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Margin="5"
            Name="txtSample" Text="Sample Text"/>

        <Label Grid.Row="1" Grid.Column="0" Content="Size:" Margin="5"/>
        <Slider Grid.Row="1" Grid.Column="1" Margin="5"
            Name="sliSize" Minimum="5" Maximum="100" Value="20"
            TickFrequency="5" TickPlacement="BottomRight"
            IsSnapToTickEnabled="True"/>
        <Label Grid.Row="1" Grid.Column="2"
            VerticalAlignment="Center"
            Content="{Binding ElementName=sliSize, Path=Value}"
            ContentStringFormat="{}{0:#}"/>

        <Button Grid.Row="2" Grid.ColumnSpan="3"
            Width="100" Height="30" Margin="5" IsDefault="True"
            Content="Display Fonts" Name="btnShowSamples"
            Click="btnShowSamples_Click"/>

        <ListBox Grid.Row="3" Grid.ColumnSpan="3"
            Margin="5" Name="lstSamples" SelectionChanged="lstSamples_SelectionChanged"/>

        <Label Grid.Row="4" Grid.ColumnSpan="3"
            Name="lblFontName" Content=" " Margin="5,0,5,0"/>
    </Grid>
</Window>

Here is the code behind.

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ListBoxDisplayFonts
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        // Display samples of the text in the available fonts.
        private void btnShowSamples_Click(object sender, RoutedEventArgs e)
        {
            lblFontName.Content = null;
            string sample = txtSample.Text;
            lstSamples.Items.Clear();
            foreach (FontFamily family in Fonts.SystemFontFamilies)
            {
                Label label = new Label();
                label.Content = sample;
                label.Content = family + "   " + sample;
                label.FontFamily = family;
                label.FontSize = sliSize.Value;
                lstSamples.Items.Add(label);
            }
        }
        // Display the name of the clicked font.
        private void lstSamples_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Label label = lstSamples.SelectedItem as Label;
            if (label == null)
                lblFontName.Content = null;
            else
                lblFontName.Content = label.FontFamily.ToString();
        }
    }
}
Series Navigation<< WPF ListBox of Colors