question

ApptacularApps-8305 avatar image
0 Votes"
ApptacularApps-8305 asked FayWang-MSFT commented

How to set text of existing TextBlock dynamically

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";



windows-uwp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, can the following method help? Do you have other questions?

0 Votes 0 ·

1 Answer

FayWang-MSFT avatar image
0 Votes"
FayWang-MSFT answered

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.