Search code examples
textbindingwin-universal-apppartialtextblock

Binding Part of 'TextBlock' in UWP


Any idea on how to only bind part of 'TextBlock.Text' string.

For example:

I want to add the "Your name is {Binding Path=Name}" and have 'OneWay Binding' without having to use another 'TextBlock' control.


Solution

  • Put the text in your Bindings into Runs:

    <TextBlock>
        <Run>Your name is</Run>
        <Run Text="{Binding Path=Name}"/>
    </TextBlock>
    

    It's possible that you run into issues with the whitespaces between the Runs in such a case. You can use the attribute xml:space="preserve" on TextBlock in this case.