24

I was wondering something, and couldn't find any relevant topics. I have following binding :

Content="{x:Static resx:Resource.Form_OtherOption_Description}"

This will place a string in a label. What i was asking myself is if i can add a ":" after that binding, not in code, just in xaml. The label represent something like "Name :". But adding the ":" as part of the binding is not an option.

Edit

I'm working in 3.5 version

Any suggestions.

Thanks in advance.

Terry
  • 5,132
  • 4
  • 33
  • 66

7 Answers7

34

You could accomplish this with something like:

<TextBlock Text="{Binding Source={x:Static resx:Resource.Form_OtherOption_Description},
                         StringFormat={}{0}:}" />

Edit: <Label>s Content property does not respect the StringFormat property of a binding apparently. Which I've found has been moved to the ContentStringFormat property on the <Label>.

<Label Content="{x:Static resx:Resource.Form_OtherOption_Description}"
       ContentStringFormat="{}{0}:" />
user7116
  • 63,008
  • 17
  • 141
  • 172
  • 1
    +1, Saw you found the answer as well :) So `ContentControl` has `ContentStringFormat`, `HeaderedContentControl` has `HeaderStringFormat`, and `ItemsControl` has `ItemStringFormat`. Good to know – Fredrik Hedblad Feb 09 '11 at 16:47
  • 2
    Appears that `Label.Content` does not pick up the `StringFormat` from the binding because the `TargetType` is not `string`, but instead `object`. In the case of `TextBlock.Text`, the target type is `string`, and thus it is used in the binding. – user7116 Feb 09 '11 at 17:44
  • 1
    Good spot - although not as elegent you can add a TextBlock to the labels content, but this would be additional overhead. – Andrew Feb 09 '11 at 17:45
22

If you're using WPF 4.0, you could also do this:

<TextBlock>
       <Run Text="{Binding SomeLabel}"/>
       <Run Text=":"/>
</TextBlock>

This actually concatenates the two strings coming from two Run tag and copied into TextBlock.Text property!.

Using this approach you can even bind to different properties in presenter, and display it in a single TexBlock. See this excellent example:

Can we concat two properties in data binding?

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
10

You can also use MultiBinding with StringFormat e.g:

<TextBlock>
    <TextBlock.Text>
        <MultiBinding StringFormat="ID {0} Name: {1} Age: {2}">
            <Binding Source="{x:Static resx:SomeResx.ID}"/>
             <Binding Path="Name"/>
             <Binding Path="Age"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

You can use this in a content control TextBlock TextBlock.Text (sorry I couldn't get the code to show up for this above)

user7116
  • 63,008
  • 17
  • 141
  • 172
Andrew
  • 2,598
  • 16
  • 27
  • As per sixlettervariables answer above the label uses a contentStringFormat. To get the above multibindings to work with a label assign the TextBlock to its content. – Andrew Feb 09 '11 at 17:42
6

Yes you can. Here I add "testing" after binding text(clouds.all) in windows phone.

<TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}testing}"/>
reza.cse08
  • 5,938
  • 48
  • 39
3

Try Binding's property StringFormat - it can do very simply what you want.

Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
2

if you use a label inside a progress bar you can use this way:

<Label x:Name="Progress" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontWeight="Bold" Foreground="White" Opacity=".7" 
       Content="{Binding Path=Value, RelativeSource={RelativeSource TemplatedParent}}" ContentStringFormat="{}{0}%">

in this way you can visualize the value of progressbar with a % added.

luka
  • 605
  • 8
  • 15
0

You can create a converter that takes the input string and adds the ":".

public class AddStringToStringConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string input = value as string;
        string suffix = parameter as string;

        return input + suffix;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

Xaml:

<Window.Resources>
    <local:AddStringToStringConverter x:Key="AddStringToStringConverter"/>
</Window.Resources>
...
<Label Text="{Binding Source={x:Static resx:Resource.Form_OtherOption_Description}, Converter={StaticResource AddStringToStringConverter}, ConverterParameter=:}"/>

Or something like that. Tried it and it worked for my source at least.

If you have whitespace and the like in you ConverterParameter you can use signle quotes to make sure it does not get disposed.

Edit: Oh right... yeah... there's also StringFormat which i have never needed before, ehehehe...

H.B.
  • 166,899
  • 29
  • 327
  • 400