Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stringformat concatenates databinding and resource's value

I want to concatenate in my window title a property from my viewmodel and a value that cames from a resources file. This is what I have working without the string from resources:

Title="Binding Path=Description, StringFormat=Building: {0}}"

Now I want to remove the "Building" string and put a value from a resource like I use on other places:

xmlns:res="clr-namespace:Project.View.Resources"
{res:Strings.TitleDescription}

How can I define both? Can I define like a {1} parameter?

like image 955
Louro Avatar asked Aug 07 '12 18:08

Louro


2 Answers

I've seen the MultiBinding answer in several places now, and it is almost never necessary to use it. You can define your resource as the string format instead, and as long as there is only one string format argument, no MultiBinding is required. Makes the code a lot more succinct:

<TextBlock Text="{Binding Description, StringFormat={x:Static res:Strings.TitleDesc}}" />

And the TitleDesc resource is obviously "Building: {0}".

like image 87
Mike Fuchs Avatar answered Oct 20 '22 05:10

Mike Fuchs


Yes, you can. Simply use a MultiBinding.

The MSDN article on StringFormat has an example.

In your case, the code would look something like this:

  <TextBlock>
    <TextBlock.Text>
      <MultiBinding  StringFormat="{}{0} {1}">
        <Binding Source="{x:Static res:Strings.TitleDescription}"/>
        <Binding Path="Description"/>
      </MultiBinding>
    </TextBlock.Text>
  </TextBlock>
like image 36
madd0 Avatar answered Oct 20 '22 06:10

madd0