accordo777
accordo777

Reputation: 407

WPF Localization of dynamic string in TextBlock

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!

Upvotes: 1

Views: 653

Answers (2)

mm8
mm8

Reputation: 169160

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>

Upvotes: 3

Malior
Malior

Reputation: 1341

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#)

Upvotes: 2

Related Questions