2

I need to get a string from View Model and another from Resx and concatenate them to show it in a label. I know I can do it from View model by creating a new property. However, I was wondering if there is a way to it in XAML itself. Something like this

  <Label Text="{Binding UserName, StringFormat='{i18n:Translate Welcome} {0}'}"/>
Katana
  • 401
  • 3
  • 22

1 Answers1

1

You can simply use a formatted string in your label, aiding yourself with spans and building up your Label Text, spans now allow you to do bindings in their text property so if you do the following it should work.

<Label>
    <Label.FormattedText>
        <FormattedString>
            <Span Text="{i18n:Translate Welcome}"/>
            <Span Text="{Binding UserName}"/>
        </FormattedString>
    </Label.FormattedText>
</Label>
Pujolsluis
  • 38
  • 5
  • 1
    `FormattedString` does not account for the word order in a language. E.g. `Replace XXX with this` in English would become `XXXをこれに置き換えます` in Japanese. `FormattedString` would probably work for the above example, but I don't think it would be a general solution for string formatting. – Patrick Aug 12 '20 at 14:53