Question

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?

Was it helpful?

Solution

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>

OTHER TIPS

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}".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top