Encore une approche à essayer. Utilisez TextBlock.Inlines. Ensuite, liez votre modèle au TextBlock et, soit dans la propriété personnalisée value converter, soit via une propriété jointe personnalisée, analysez votre modèle pour remplir les inlines de TextBlock.
Voici un exemple de la propriété Attached qui prend chaîne de texte et fait chaque deuxième mot gras:
public class RunExtender : DependencyObject
{
public static string GetText(DependencyObject obj)
{
return (string)obj.GetValue(TextProperty);
}
public static void SetText(DependencyObject obj, string value)
{
obj.SetValue(TextProperty, value);
}
public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached("Text", typeof(string), typeof(RunExtender), new PropertyMetadata(string.Empty, OnBindingTextChanged));
private static void OnBindingTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var bindingText = e.NewValue as string;
var text = d as TextBlock;
if (text != null)
{
text.Inlines.Clear();
var words = bindingText.Split(' ');
for (int i = 0; i < words.Length; i++)
{
var word = words[i];
var inline = new Run() {Text = word + ' '};
if (i%2 == 0)
{
inline.FontWeight = FontWeights.Bold;
}
text.Inlines.Add(inline);
}
}
}
}
Ce n'est pas un code de qualité de la production, et il est pris de Silverlight démo, mais vous voyez l'idée.
Espérons que cela aide.
Cheers, Anvaka.