WPF Label


The WPF Label control is a content control that doesn’t have the notion of being clicked (like a button). The Label is really only useful for displaying text, but because it is a content control, it can hold arbitrary content in its Content property. Placing text on the screen can be done with the TextBlock as well (and others), but Label supports access keys. You can designate any letter in a Label’s text so that when the user presses the access key, the Alt key and the designated letter, an element of your choice will get the focus. To designate the letter, simply preceded it with an underscore.

Below is the classic case of pairing a Label with a TextBox.

Below is the XAML code.

<Window x:Class="LabelSimple.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:LabelSimple"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="LabelSimple" Height="80" Width="200">
    <StackPanel Orientation="Horizontal" Height="25">
        <Label Target="nameTextBox">_Name:</Label>
        <TextBox x:Name="nameTextBox" MinWidth="100"/>
    </StackPanel>
</Window>

There are many settings you can provide to your label. Here are some of them.

  • FontSize=”12″
  • FontWeight=”Normal”
  • FontFamily
  • Background=”Black”
  • Foreground=”Orange”
  • VerticalAlignment=”Center”
  • HorizontalAlignment=”Center”