Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Localization: DynamicResource with StringFormat?

I am doing localization in .NET 4 with a ResourceDictionary. Does anyone have a solution for using a value with string format?

For instance, let's say I have a value with the key "SomeKey":

<ResourceDictionary ...>
    <s:String x:Key="SomeKey">You ran {0} miles</s:String>
</ResourceDictionary>

Using it in a TextBlock:

<TextBlock Text="{DynamicResource SomeKey}" />

How would I combine, for example, an integer with the value of SomeKey as a format string?

like image 276
Andy Clark Avatar asked Jun 18 '15 20:06

Andy Clark


1 Answers

You need to bind to a ViewModel.Value somehow, and then use a (nested) binding to a format string.

When you have only one value:

<TextBlock 
  Text="{Binding Path=DemoValue, StringFormat={StaticResource SomeKey}}" />        

When you also have {1} etc then you need MultiBinding.

Edit:

When you really want to change languages in a live Form then the sensible way is probably to do all formatting in the ViewModel. I rarely use StringFormat or MultiBinding in MVVM anyway.

like image 170
Henk Holterman Avatar answered Oct 17 '22 03:10

Henk Holterman