How to add text to a WPF Label in code?

C#Wpf

C# Problem Overview


I feel stupid but cannot find out how to add a text to a WPF Label control in code. Like following for a TextBlock:

DesrTextBlock.Text = "some text";

What is equivalent property in Label for doing it?

DesrLabel.??? = "some text"; //something like this

C# Solutions


Solution 1 - C#

Try DesrLabel.Content. Its the WPF way.

Solution 2 - C#

In normal winForms, value of Label object is changed by,

myLabel.Text= "Your desired string";

But in WPF Label control, you have to use .content property of Label control for example,

myLabel.Content= "Your desired string";

Solution 3 - C#

I believe you want to set the Content property. This has more information on what is available to a label.

Solution 4 - C#

You can use the Content property on pretty much all visual WPF controls to access the stuff inside them. There's a heirarchy of classes that the controls belong to, and any descendants of ContentControl will work in this way.

Solution 5 - C#

you can use TextBlock control and assign the text property.

Solution 6 - C#

Label myLabel = new Label (); myLabel.Content = "Hello World!";

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
QuestionremView Question on Stackoverflow
Solution 1 - C#Daniel A. WhiteView Answer on Stackoverflow
Solution 2 - C#Utkal SinhaView Answer on Stackoverflow
Solution 3 - C#Mark AveniusView Answer on Stackoverflow
Solution 4 - C#RichardW1001View Answer on Stackoverflow
Solution 5 - C#Davide PirasView Answer on Stackoverflow
Solution 6 - C#mitoView Answer on Stackoverflow