How to center a label text in WPF?

C#.NetWpf

C# Problem Overview


How to center a label text in WPF?


Label HorizontalAlignment="Center" Content="What?" FontSize="25" FontWeight="Bold" Canvas.Top="5"


C# Solutions


Solution 1 - C#

use the HorizontalContentAlignment property.

Sample

<Label HorizontalContentAlignment="Center"/>

Solution 2 - C#

The Control class has HorizontalContentAlignment and VerticalContentAlignment properties. These properties determine how a control’s content fills the space within the control.
Set HorizontalContentAlignment and VerticalContentAlignment to Center.

Solution 3 - C#

You have to use HorizontalContentAlignment="Center" and! Width="Auto".

Solution 4 - C#

Sample:

Label label = new Label();
label.HorizontalContentAlignment = HorizontalAlignment.Center;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionalansiqueira27View Question on Stackoverflow
Solution 1 - C#bijuView Answer on Stackoverflow
Solution 2 - C#Akshay JView Answer on Stackoverflow
Solution 3 - C#KombinatorView Answer on Stackoverflow
Solution 4 - C#NewredView Answer on Stackoverflow