How to set text of existing TextBlock dynamically

Apptacular Apps 386 Reputation points
2020-06-11T22:32:09.07+00:00

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";
Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Fay Wang - MSFT 5,221 Reputation points
    2020-06-12T02:05:25.32+00:00

    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
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.