Search code examples
wpfxamlstring-formattingresx

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 ... :(


Solution

  • 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 "+++ "