TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to WPF

WPF Label Example

Add the WPF Label control and handle Label_Loaded. Access Label attributes.
Label. A Label displays text. We can apply styling to it. We can dynamically change the Content of a Label. The Label_Loaded event handler allows to changes its attributes at WPF program startup.
First, please create a new WPF project, and drag a Label to the designer window. Next change the markup of the XAML file—this is where the controls are specified. Modify the Label element.

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.

Also, we create the Label_Loaded event. Please type "Label" into the XAML file and then have Visual Studio auto-generate the C# event handler. This is the easiest way to modify a label in a dynamic way.
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(); } } }
In Label_Loaded, we assign the Content of the Label to a string. We get the reference to the Label Control by casting the argument "sender" to the Label type. We use DateTime.Now to get the current day.AsDateTime.Now
Loaded. Why do we use the Label_Loaded event handler to assign a value to the Label? A static (constant) label can be assigned directly within the XAML. But for a dynamic label, one that depends on external factors, we require C# code.

And: The Label_Loaded event handler provides a convenient way to access the Label at program startup, and modify its Content.

TextBlock, Label. What is the difference between the TextBlock element and the Label element? The TextBlock element by default has attributes for longer text. It provides the TextWrapping attribute. Label has no TextWrapping attribute.

So: The TextBlock is for a larger "block" of text. The Label is for a shorter, "labeling" piece of text.

Summary. Some things in life are certain: including death and taxes. In a WPF program, the need to use visual, text labels on controls is nearly as certain. We used the Label control and its Label_Loaded event.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf