Stringformat concatenates databinding and resource's value
Asked Answered
C

2

17

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?

Custos answered 7/8, 2012 at 18:38 Comment(0)
S
25

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>
Slyke answered 7/8, 2012 at 18:41 Comment(6)
Multibinding to a static resource? never heard of it :SCustos
I may have misunderstood then, what do you mean by "static resource"? Is it a static property of a class? If that's the case, I believe support for binding those is new in .NET 4.5Slyke
I've updated the question. I want to use a value from my .resx Resource. I don't think I can do that with a binding...Custos
I changed the example added by Rachel to show what the code would look like in your case.Slyke
Please include the xmlns for "res", I'm not sure what that should look like.Pargeting
@Okuma.Scott It points to whatever namespace your resource type is in, something like xmlns:res="clr-namespace:MyApp.Properties". Also, make sure the access modifier for your resources is set to public, otherwise WPF won't see it.Slyke
S
28

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

Syndesis answered 1/12, 2015 at 14:6 Comment(3)
Upvote for this answer. Much simpler than using MultiBindingsGeometric
Thanks I was doing the mistake of prefixing the string in resources with the {} .Also I wanted something like \\Path\{0} so what I ended up putting into .resx file was \\\\Path\\{0} - needed to escape the backslashesSupper
Exactly what I was looking for. You can also add the Resource as FallbackValue to have it shown in the Designer.Flora
S
25

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>
Slyke answered 7/8, 2012 at 18:41 Comment(6)
Multibinding to a static resource? never heard of it :SCustos
I may have misunderstood then, what do you mean by "static resource"? Is it a static property of a class? If that's the case, I believe support for binding those is new in .NET 4.5Slyke
I've updated the question. I want to use a value from my .resx Resource. I don't think I can do that with a binding...Custos
I changed the example added by Rachel to show what the code would look like in your case.Slyke
Please include the xmlns for "res", I'm not sure what that should look like.Pargeting
@Okuma.Scott It points to whatever namespace your resource type is in, something like xmlns:res="clr-namespace:MyApp.Properties". Also, make sure the access modifier for your resources is set to public, otherwise WPF won't see it.Slyke

© 2022 - 2024 — McMap. All rights reserved.