Se si sta utilizzando 3.5 SP1, è possibile utilizzare la proprietà StringFormat
sul legame:
<Label Content="{Binding Order.ID, StringFormat=Order ID \{0\}}"/>
In caso contrario, utilizzare un convertitore:
<local:StringFormatConverter x:Key="StringFormatter" StringFormat="Order ID {0}" />
<Label Content="{Binding Order.ID, Converter=StringFormatter}"/>
Con StringFormatConverter
essere un 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();
}
Questo farà il trucco.
[Edit: Modificare la proprietà Text
a Content
]
Con l'esempio stringformat, cosa succede se la stringa deve contenere una virgola?Non hai altra scelta che andare con l'opzione del convertitore? –
AFAIK il controllo etichetta non ha la proprietà 'Text'. È necessario associare alla proprietà 'Content' –