WPF - Controls-Label - Windows Presentation Foundation(WPF)

What is WPF Label?

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 –

hierarchy_of_label

Below are the commonly used properties of Label class

Given below are the most commonly used properties of Label.

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

Given below are the most commonly used events of Label.

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

Given below are the most commonly used methods of Label.

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.

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

output_of_label

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Windows Presentation Foundation(WPF) Topics