Universal Windows Platform (UWP)
A Microsoft platform for building and publishing apps for Windows desktop devices.
2,971 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
What's the correct way to find and set the text for an existing TextBlock
dynamically? I can't seem to find the relevant tutorial anywhere.
XAML
<TextBlock x:Name="PageTitle" Style="{StaticResource BodyTextBlockStyle}" />
C#
var txtTitle =???;
txtTitle.Text = "Hello World";
Hello,
Welcome to Microsoft Q&A!
The x:Name uniquely identifies object elements for access to the instantiated object from code-behind or general code. So if you want to get or set the text of TextBlock in code-behind, just using the specified x:Name which holds a reference to the TextBlock. For example:
.cs:
string txtTitle = PageTitle.Text; //get Text
PageTitle.Text = "Hello World"; //set Text