[ACCEPTED]-Is it possible to add more characters after a binding in xaml?-binding

Accepted answer
Score: 34

You could accomplish this with something 3 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 2 of a binding apparently. Which I've found 1 has been moved to the ContentStringFormat property on the <Label>.

<Label Content="{x:Static resx:Resource.Form_OtherOption_Description}"
       ContentStringFormat="{}{0}:" />
Score: 22

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

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

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

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

Can we concat two properties in data binding?

Score: 10

You can also use MultiBinding with StringFormat 3 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 2 TextBlock TextBlock.Text (sorry I couldn't 1 get the code to show up for this above)

Score: 6

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

<TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}testing}"/>
Score: 3

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

Score: 2

if you use a label inside a progress bar 3 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 2 visualize the value of progressbar with 1 a % added.

Score: 0

You can create a converter that takes the 7 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 6 like that. Tried it and it worked for my 5 source at least.

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

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

More Related questions