parfum
parfum

Reputation: 347

How can I use StringFormat in Resx text (WPF .xaml)?

I want to use text string in Resx file and add another text. For example,

 <Label Content="{Resx example}" ContentStringFormat="+++ {0}"

If I use like this, it display "+++ Example" . ('Example' is in resx file.)

But, if i try to use TextBlock, I can't use StringFormat. For example,

<TextBlock Text="{Resx example, StringFormat="+++ {0}"} ...

I can't code like that. How can I write additional Text with Resx? Please help me ... :(

Upvotes: 1

Views: 2532

Answers (4)

snidelys4
snidelys4

Reputation: 1

(Late answer, I know, but for anyone else who might check here...)

The instructions in the CodeProject page for "WPF Localization Using RESX Files" (https://www.codeproject.com/articles/35159/wpf-localization-using-resx-files) has a section "Formatting Bound Data" that addresses this. Basically look into BindingElementName and BindingPath...

The examples they give:

<Resx Key="MyFormatString" BindingElementName="_fileListBox" BindingPath="SelectedItem"/>

and

<Resx Key="MyMultiFormatString">
    <Resx BindingElementName="_fileListBox" BindingPath="Name"/>
    <Resx BindingElementName="_fileListBox" BindingPath="SelectedItem"/>
</Resx> 

Upvotes: 0

Felix D.
Felix D.

Reputation: 5103

Since you are trying to localize ur application this may become handy:

I my project I got a folder called 'Strings' for all resx like Strings.de.resx, Strings.fr.resx, etc.

then go add

xmlns:loc="<yourPathToYourFolderContainigResxFiles>"

Then bind like this:

<TextBlock Text="{x:Static loc:Strings.TEST}" />

Note:

  • To get intellisens on Strings.xxx you need to have your resx compiled so build first.
  • This will automatically choose accoring to CurrentUICulture of your Thread.
  • Access Modifier needs to be public !

enter image description here

This is my MainWindow:

enter image description here

Since this is not a Binding you cannot use StringFormat here.

So you either need to add a property to your ViewModel which is holding your values or you need to place the full string in your resx.

Upvotes: 0

Pavel
Pavel

Reputation: 910

It looks like 'Infralution localization wpf dll' does not support this functionality. But as it an open source project, you can grab its source code and modify the ResxExtension class. You will have to add one more property (StringFormat) and modify GetValue method to use it. Or, if you do not want to edit the source code, you can just create your own markup extension inherited from ResxExtension, add the property and override the GetValue method.

But for the simple cases, when you do not need complex formatting, you can try to go another way. As the TextBlock actually contains a collection of inline elements, you can try to use it in the following way:

<TextBlock>
    <Run Text="+++" />
    <Run Text="{Resx example, BindingMode=OneWay}" />
</TextBlock>

This way you will also get the string from the "example" resource prepended with "+++ "

Upvotes: 1

ivica.moke
ivica.moke

Reputation: 1064

I think you are only missing one {} before +++ to make that string format work, something like

<TextBlock Text="{Binding example,StringFormat={}+++{0}}"/>

Upvotes: 2

Related Questions