LPC*_*Roy 51
Binding.StringFormat属性不适用于标签,您需要在Label上使用ContentStringFormat属性.
例如,以下示例将起作用:
<Label>
<Label.Content>
<Binding Path="QuestionnaireName"/>
</Label.Content>
<Label.ContentStringFormat>
Thank you for taking the {0} questionnaire
</Label.ContentStringFormat>
</Label>
Run Code Online (Sandbox Code Playgroud)
虽然这个样本不会:
<Label Content="{Binding QuestionnaireName}" ContentStringFormat="Thank you for taking the {0} questionnaire" />
Run Code Online (Sandbox Code Playgroud)
Inf*_*ris 23
如果您使用的是3.5 SP1,则可以StringFormat在绑定上使用该属性:
<Label Content="{Binding Order.ID, StringFormat=Order ID \{0\}}"/>
Run Code Online (Sandbox Code Playgroud)
否则,请使用转换器:
<local:StringFormatConverter x:Key="StringFormatter" StringFormat="Order ID {0}" />
<Label Content="{Binding Order.ID, Converter=StringFormatter}"/>
Run Code Online (Sandbox Code Playgroud)
随着StringFormatConverter作为一个IValueConverter:
[ValueConversion(typeof(object), typeof(string))]
public class StringFormatConverter : IValueConverter
{
public string StringFormat { get; set; }
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture) {
if (string.IsNullOrEmpty(StringFormat)) return "";
return string.Format(StringFormat, value);
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
Run Code Online (Sandbox Code Playgroud)
那就行了.
[ 编辑:将Text属性更改为Content]
例如,经常被忽视的只是简单地将多个文本块链接在一起
<TextBlock Text="{Binding FirstName}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding LastName}" />
Run Code Online (Sandbox Code Playgroud)