XAML Label Code Example

A XAML Label element represents a Label control. Code sample of how to use a XAML Label. 
 
The code example in Listing 1 creates a Label control in XAML and sets the name, height, width and content of a Label control. The code also sets the font format for the text. 
  1. <Label Name="Label1"  
  2.     Content="Hello! I am Label Control"   
  3.     Width="200" Height="40"  
  4.     Canvas.Left="10" Canvas.Top="10"   
  5.     FontSize="14" FontFamily="Georgia"  
  6.     FontWeight="Bold"/>  
Listing 1

The output looks as in Figure 1.

Figure 1

The Background and Foreground properties set the background and foreground colors of the Label control. The code snippet in Listing 2 sets the background, foreground and alignment of a Label control.
  1. <Label Name="Label1"  
  2.     Content="Hello! I am Label Control"   
  3.     Width="200" Height="30"  
  4.     Canvas.Left="10" Canvas.Top="10"   
  5.     FontSize="14" FontFamily="Georgia"  
  6.     FontWeight="Bold"   
  7.     Background="Black"   
  8.     Foreground="Orange"   
  9.     VerticalAlignment="Center"   
  10.     HorizontalAlignment="Center"/>  
Listing 2

The new output looks as in Figure 2.



Figure 2

Formatting a Label

The BorderBrush property of the Label sets a brush to draw the border of a Label. You may use any brush to fill the border. The following code snippet uses a linear gradient brush to draw the border with a combination of Red and Blue colors.
  1. <Label.BorderBrush>  
  2.     <LinearGradientBrush StartPoint="0,0" EndPoint="1,1" >  
  3.         <GradientStop Color="Blue" Offset="0" />  
  4.         <GradientStop Color="Red" Offset="1.0" />  
  5.     </LinearGradientBrush>  
  6. </Label.BorderBrush>  
The Background and Foreground properties of the Label set the background and foreground colors of a Label. You may use any brush to fill the border. The following code snippet uses linear gradient brushes to draw the background and foreground of a Label.
  1. <Label.Background>  
  2.     <LinearGradientBrush StartPoint="0,0" EndPoint="1,1" >  
  3.         <GradientStop Color="Blue" Offset="0.1" />  
  4.         <GradientStop Color="Orange" Offset="0.25" />  
  5.         <GradientStop Color="Green" Offset="0.75" />  
  6.         <GradientStop Color="Black" Offset="1.0" />  
  7.     </LinearGradientBrush>  
  8. </Label.Background>  
  9. <Label.Foreground>  
  10.     <LinearGradientBrush StartPoint="0,0" EndPoint="1,1" >  
  11.         <GradientStop Color="Orange" Offset="0.25" />  
  12.         <GradientStop Color="Green" Offset="1.0" />  
  13.     </LinearGradientBrush>  
  14. </Label.Foreground>  
The new Label looks as in Figure 4.



Figure 4

Setting Image as Background of a Label

To set an image as background of a Label, we can set an image as the Background of the Label. The following code snippet sets the background of a Label to an image.
  1. <Label.Background>  
  2.     <ImageBrush ImageSource="Garden.jpg" />  
  3. </Label.Background>  
The new output looks as in Figure 5.



Figure 5

To learn more about Label control, pelase visit WPF Label Control 


Recommended Free Ebook
Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.