Il existe différents Inline
éléments qui peuvent vous aider, pour les options de formatage les plus simples que vous pouvez utiliser Bold
, Italic
et Underline
:
<TextBlock>
Sample text with <Bold>bold</Bold>, <Italic>italic</Italic> and <Underline>underlined</Underline> words.
</TextBlock>

Je pense qu'il vaut la peine de noter que ces éléments ne sont en fait que des raccourcis pour les Span
éléments avec diverses propriétés définies (c'est-à-dire: pour Bold
, la FontWeight
propriété est définie sur FontWeights.Bold
).
Cela nous amène à notre prochaine option: l' Span
élément susmentionné .
Vous pouvez obtenir les mêmes effets avec cet élément que ci-dessus, mais vous avez encore plus de possibilités; vous pouvez définir (entre autres) la Foreground
ou les Background
propriétés:
<TextBlock>
Sample text with <Span FontWeight="Bold">bold</Span>, <Span FontStyle="Italic">italic</Span> and <Span TextDecorations="Underline">underlined</Span> words. <Span Foreground="Blue">Coloring</Span> <Span Foreground="Red">is</Span> <Span Background="Cyan">also</Span> <Span Foreground="Silver">possible</Span>.
</TextBlock>

L' Span
élément peut également contenir d'autres éléments comme celui-ci:
<TextBlock>
<Span FontStyle="Italic">Italic <Span Background="Yellow">text</Span> with some <Span Foreground="Blue">coloring</Span>.</Span>
</TextBlock>

Il y a un autre élément, qui est assez similaire à Span
, il s'appelle Run
. Le Run
ne peut pas contenir d'autres éléments en ligne tandis que le Span
can, mais vous pouvez facilement lier une variable à la propriété Run
's Text
:
<TextBlock>
Username: <Run FontWeight="Bold" Text="{Binding UserName}"/>
</TextBlock>

En outre, vous pouvez faire tout le formatage à partir du code-behind si vous préférez:
TextBlock tb = new TextBlock();
tb.Inlines.Add("Sample text with ");
tb.Inlines.Add(new Run("bold") { FontWeight = FontWeights.Bold });
tb.Inlines.Add(", ");
tb.Inlines.Add(new Run("italic ") { FontStyle = FontStyles.Italic });
tb.Inlines.Add("and ");
tb.Inlines.Add(new Run("underlined") { TextDecorations = TextDecorations.Underline });
tb.Inlines.Add("words.");