C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Alignment: We change the HorizontalAlignment of the Label to be Stretch—this makes the label expand its width.
Background: We use the exciting color Beige as the background (back color) of the Label control.
Example markup: XAML
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label
Content="Label"
HorizontalAlignment="Stretch"
Margin="10,10,10,10"
VerticalAlignment="Center"
Background="Beige"
Padding="5"
FontSize="30"
Loaded="Label_Loaded"/>
</Grid>
</Window>
Example code: C#
using System;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Label_Loaded(object sender, RoutedEventArgs e)
{
// ... Get label.
var label = sender as Label;
// ... Set date in content.
label.Content = DateTime.Now.ToShortDateString();
}
}
}
And: The Label_Loaded event handler provides a convenient way to access the Label at program startup, and modify its Content.
So: The TextBlock is for a larger "block" of text. The Label is for a shorter, "labeling" piece of text.