Debhere Debhere - 4 months ago 21
C# Question

How to format a string with a value from resources and one value from Binding in XAML?

I need to develop an application that supports localization, so I need to format a

TextBlock
in such a way that part of the string takes one parameter from binding and part from a resources. I have written like this

<TextBlock Height="30" >
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} -- *Now only* {1:C}!">
<Binding Path="Description"/>
<Binding Path="Price"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>


this Now Only part I need to fetch from resource string. The string is there in the resources but I don't know how to fromat the string in such a fashion. Can anyone help me please.

I know that using a converter would be a solution, but I have many such scenarios and for that I may end up with a multiple converters which is not a good idea. I believe that using a
StringFormat
I can achieve it. But I am missing something.

Answer

You could add a reference to your Resources namespace then add another Binding to you MultiBinding

Example (untested)

   xmlns:p="clr-namespace:MyApplication.Properties"

   <TextBlock Height="30" >
        <TextBlock.Text>
            <MultiBinding  StringFormat="{}{0} -- {2} {1:C}!">
                <Binding Path="Description"/>
                <Binding Path="Price"/>

                <Binding Path="{x:Static p:Resources.Nowonly}"/>
                <!-- Or possibly Source instaed of Path as mentioned by @dkozl -->
                <Binding Source="{x:Static p:Resources.Nowonly}"/>
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>