Is it possible to add more characters after a binding in xaml?
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开发者_如何学JAVA not an option.
Edit
I'm working in 3.5 version
Any suggestions.
Thanks in advance.
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}:" />
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?
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)
Yes you can. Here I add "testing" after binding text(clouds.all) in windows phone.
<TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}testing}"/>
Try Binding's property StringFormat - it can do very simply what you want.
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.
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...
精彩评论