WPF - Label


Advertisements

The Label class provides both functional and visual support for access keys (also known as mnemonics). It is frequently used to enable quick keyboard access to controls. The hierarchical inheritance of Label class is as follows −

Hierarchical of Label

Below are the commonly used properties of Label class

Sr.No. Property & Description
1

Background

Gets or sets a brush that describes the background of a control. (Inherited from Control.)

2

Content

Gets or sets the content of a ContentControl. (Inherited from ContentControl.)

3

ContentStringFormat

Gets or sets a composite string that specifies how to format the Content property if it is displayed as a string.(Inherited from ContentControl.)

4

ContentTemplate

Gets or sets the data template used to display the content of the ContentControl. (Inherited from ContentControl.)

5

ContextMenu

Gets or sets the context menu element that should appear whenever the context menu is requested through user interface (UI) from within this element.(Inherited from FrameworkElement.)

6

FontFamily

Gets or sets the font family of the control. (Inherited from Control.)

7

FontSize

Gets or sets the font size. (Inherited from Control.)

8

FontStyle

Gets or sets the font style. (Inherited from Control.)

9

FontWeight

Gets or sets the weight or thickness of the specified font. (Inherited from Control.)

10

Foreground

Gets or sets a brush that describes the foreground color. (Inherited from Control.)

11

Height

Gets or sets the suggested height of the element. (Inherited from FrameworkElement.)

12

Margin

Gets or sets the outer margin of an element. (Inherited from FrameworkElement.)

13

Name

Gets or sets the identifying name of the element. The name provides a reference so that code-behind, such as event handler code, can refer to a markup element after it is constructed during processing by a XAML processor. (Inherited from FrameworkElement.)

14

Resources

Gets or sets the locally-defined resource dictionary. (Inherited from FrameworkElement.)

15

Style

Gets or sets the style used by this element when it is rendered. (Inherited from FrameworkElement.)

16

Target

Gets or sets the element that receives focus when the user presses the label's . GTMT

17

Template

Gets or sets a control template. (Inherited from Control.)

18

Width

Gets or sets the width of the element. (Inherited from FrameworkElement.)

Commonly Used Events of Label Class

Sr.No. Event & Description
1

ContextMenuOpening

Occurs when the system processes an interaction that displays a context menu.

2

DragEnter

Occurs when the input system reports an underlying drag event with this element as the target. (Inherited from UIElement)

3

DragLeave

Occurs when the input system reports an underlying drag event with this element as the origin. (Inherited from UIElement)

4

DragOver

Occurs when the input system reports an underlying drag event with this element as the potential drop target. (Inherited from UIElement)

5

Drop

Occurs when the input system reports an underlying drop event with this element as the drop target. (Inherited from UIElement)

6

GotFocus

Occurs when a UIElement receives focus. (Inherited from UIElement)

7

KeyDown

Occurs when a keyboard key is pressed while the UIElement has focus. (Inherited from UIElement)

8

KeyUp

Occurs when a keyboard key is released while the UIElement has focus. (Inherited from UIElement)

9

SizeChanged

Occurs when either the ActualHeight or the ActualWidth property changes value on a FrameworkElement. (Inherited from FrameworkElement)

Commonly Used Methods in Label Class

Sr.No. Method & Description
1

Focus

Focuses the TextBlock, as if it were a conventionally focusable control.

2

ToString

Returns the string representation of a Control object. (Inherited from Control.)

Example

  • Let’s create a new WPF project with the name WPFLabelControl.

  • Drag one label control from the Toolbox.

  • Change the different properties of label from the properties window, as shown in the following XAML code.

<Window x:Class = "WPFLabelControl.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:WPFLabelControl" 
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">
	
   <Grid> 
      <Label x:Name = "label" Content = "Label Example in WPF" HorizontalAlignment = "Left"
         Margin = "71,82,0,0" VerticalAlignment = "Top" Height = "135" Width = "474" 
         Background = "#FFD6BEBE" FontFamily = "Snap ITC" FontSize = "36"
         FontStyle = "Italic" FontWeight = "Light" Foreground = "#FFBD6B6B"/> 
   </Grid> 
	
</Window>

When you compile and execute the above code, it will produce the following window.

Output of label
wpf_controls.htm
Advertisements