1

I'm currently working on a .NET Framework 4.7.1 WPF application. I need to localize a string in a TextBlock element, using standard .resx files.

The problem is, in my TextBlock, I use a dynamic resource, consisting out of a text and an increasing number (counter).

<TextBlock Text="{Binding LoadingPercent, StringFormat=Loading the app...{0:N0}%}" />

Do you know how to localize this text "Loading the app..." in XAML?

Thank you very much!

accordo777
  • 377
  • 3
  • 10
  • 2
    I believe you could bind the StringFormat as well, so you could bind the StringFormat to a property and fill it with the correct string – MindSwipe Apr 02 '19 at 08:51

2 Answers2

3

Localize only "Loading the app..." and split the TextBlock into two Run elements:

<TextBlock>
    <Run Text="{x:Static local:Resources.LoadingLabel}" />
    <Run Text="{Binding LoadingPercent, StringFormat=P0}" />
</TextBlock>
mm8
  • 163,881
  • 10
  • 57
  • 88
2

You will have to move the format string part to a resource, and use this with MultiBinding like the following:

 <TextBlock>
     <TextBlock.Text>
         <MultiBinding StringFormat="{x:Static local:Resource1.LoadTheAppFormated}">
             <Binding Path="LoadingPercent"/>
         </MultiBinding>
     </TextBlock.Text>
 </TextBlock>

EDIT: Your resource entry Resource1.LoadTheAppFormated should of course contain the whole formatted string "Loading the app...{0:N0}%". For Localizing you will then need the extra *.en.resx (How to use localization in C#)

Malior
  • 1,221
  • 8
  • 16