Stringformat concatenates databinding and resource's value

Louro picture Louro · Aug 7, 2012 · Viewed 8.6k times · Source

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?

Answer

madd0 picture madd0 · Aug 7, 2012

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>