Controls

Label

Label is a single-content caption control. It hosts strings, visuals, or templated data, can be associated with another control through Target, and presents its content through a template that applies background, border, padding, and content alignment.

Quick start

Use Label for field captions, setting names, small status surfaces, and any other UI text that carries caption semantics instead of acting as raw body text.

Minimal label

<Label Content="Display name" />

Text-node content

<Label>Display name</Label>

Field label with mnemonic

<StackPanel>
  <Label Target="UserNameBox">_User name</Label>
  <TextBox x:Name="UserNameBox"
           Width="240" />
</StackPanel>

Default recommendation: choose Label when the element describes something else. Choose TextBlock when you only need text rendering.

How Label works

Label derives from ContentControl. Its content can be a string, a visual child, or a data item displayed through ContentTemplate. The control itself is not focusable; when access-key content is present and a target is assigned, activation is routed to the target control.

Content surface

Content is the primary payload. You can provide that payload directly, through a text node, or through binding.

Caption semantics

Target establishes the labeled control. That relationship is used by access-key routing and automation metadata.

  • Content accepts strings, visuals, and data objects.
  • Target references the control that the label describes.
  • Padding, Background, BorderBrush, and BorderThickness are consumed by the default template.
  • HorizontalContentAlignment and VerticalContentAlignment control content placement inside the label surface.

Content model

Label behaves like a normal single-content control. The content path you choose depends on whether you want plain text, custom visuals, or a reusable data template.

String content

Assign a string through Content or use a text node in XAML. This is the most common form for field captions and simple status labels.

Visual content

You can host a child element directly, such as a TextBlock, AccessText, or a compact icon-and-text composition.

  • Use string content for ordinary captions.
  • Use a child visual when you need fine control over the rendered structure.
  • Use ContentTemplate when the label content comes from data and should follow a reusable visual pattern.
  • Whitespace-only text nodes are ignored by the XAML loader.

Data-templated label content

<Label Content="{Binding ActiveProfile}"
       Padding="8">
  <Label.ContentTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="Profile: "
                   FontWeight="Bold" />
        <TextBlock Text="{Binding}" />
      </StackPanel>
    </DataTemplate>
  </Label.ContentTemplate>
</Label>

Label with a content template

<Label Target="SearchBox"
       Padding="8,4"
       Background="#122233"
       BorderBrush="#31506B"
       BorderThickness="1">
  <StackPanel Orientation="Horizontal">
    <TextBlock Text="Search" />
    <TextBlock Text=" (optional)"
               Foreground="#89A9C3" />
  </StackPanel>
</Label>

Target and access keys

Label.Target connects the label to another control. When the label content includes a mnemonic marker such as _Name, the label can participate in access-key activation and route focus to the target.

Target resolution

You can assign Target with a direct object reference, with {x:Reference ...}, or with a named element such as Target="UserNameBox".

Automation

Assigning Target updates AutomationProperties.LabeledBy on the target so automation consumers can discover the relationship.

Named target

<StackPanel>
  <Label Target="EmailBox">_Email</Label>
  <TextBox x:Name="EmailBox"
           Width="260" />
</StackPanel>

x:Reference target

<StackPanel>
  <TextBox x:Name="PasswordBox"
           Width="260" />
  <Label Target="{x:Reference PasswordBox}">_Password</Label>
</StackPanel>

Explicit AccessText content

<Label Target="SaveButton"
       Padding="6,2">
  <AccessText Text="_Save" />
</Label>

<Button x:Name="SaveButton"
                Content="Save now" />

Access-key rule: use mnemonic markers only for labels that are meant to activate a target. For decorative text, prefer TextBlock or a plain label string without a mnemonic marker.

Layout and rendering

The default label template wraps the content in a bordered content host. The label measures around its content plus template chrome and arranges the content inside the padded inner rectangle.

Desired size

The desired size grows from the presented content size plus horizontal and vertical padding and border thickness.

Chrome

Background, BorderBrush, and BorderThickness render through the control template rather than through a separate wrapper.

Content placement

Padding, HorizontalContentAlignment, and VerticalContentAlignment determine where the hosted content sits inside the label surface.

  • Empty content produces a small or empty label surface depending on style setters and size constraints.
  • Text wrapping depends on the content element that ultimately renders the text, such as TextBlock or AccessText.
  • Font and foreground properties flow to generated text presentation just like other inherited text properties.

Styled caption surface

<Label Content="Connection"
       Padding="10,6"
       Background="#112031"
       BorderBrush="#2F4C66"
       BorderThickness="1"
       Foreground="#E4F1FF"
       FontWeight="SemiBold" />

Centered content inside the label

<Label Width="220"
       Height="56"
       Content="Ready"
       Padding="8"
       HorizontalContentAlignment="Center"
       VerticalContentAlignment="Center"
       Background="#122638"
       BorderBrush="#33526C"
       BorderThickness="1" />

Style targeted at labels

<Style x:Key="FieldLabelStyle" TargetType="{x:Type Label}">
  <Setter Property="Foreground" Value="#C8D9EA" />
  <Setter Property="FontSize" Value="14" />
  <Setter Property="FontWeight" Value="SemiBold" />
  <Setter Property="Margin" Value="0,0,0,4" />
  <Setter Property="Padding" Value="6,2" />
</Style>

XAML authoring rules

Because Label is a single-content control, its XAML rules are simple but strict.

Text nodes

You can write plain text inside the element body, for example <Label>Name</Label>.

Single implicit child

You can provide one implicit visual child, such as TextBlock or StackPanel.

No mixed implicit content

Do not mix non-whitespace text-node content with an implicit visual child at the same level.

Valid text-node content

<Label>_Project name</Label>

Valid visual child

<Label Target="NotesBox">
  <TextBlock Text="Notes"
             FontWeight="Bold" />
</Label>

Common patterns

Form caption

<StackPanel>
  <Label Target="EmailBox"
         Margin="0,0,0,6">_Email address</Label>
  <TextBox x:Name="EmailBox"
           Width="280" />
</StackPanel>

Small status badge

<Label Content="ONLINE"
       Padding="8,3"
       Background="#1D3A2A"
       BorderBrush="#4BA36E"
       BorderThickness="1"
       Foreground="#DDF7E6" />

Data-bound caption

<Label Content="{Binding ActiveProfileName}"
       Padding="6,2"
       Foreground="#D9E7F5" />

Notes and guidance

Use Content for simple captions. Reach for a child visual or ContentTemplate only when the label needs richer structure.

Use Target for real field labels. That keeps access keys and automation metadata aligned with the control the user is meant to interact with.

Use TextBlock for dense text-only surfaces. Lists, diagnostics panes, and other text-heavy displays usually read more clearly when they are authored as direct text elements instead of labels.

Keep implicit content unambiguous. A label body can contain text or one implicit child. If you need more structure, put that structure under a single panel or template.