テキストブロックに次のようなテキスト行があります。
「[精度]の精度で[ジェスチャ]を検出しました」
WPFで、テキストブロック内の要素の色を変更できるようにすることはできますか?テキストブロックを複数の色にすることはできますか?たとえば、赤にしたいジェスチャ名を除いて、TextBlock全体を黒にします。
これはWPFで可能ですか?
これが役立つかどうかを確認します。
<TextBlock>
Detected
<TextBlock Text="{Binding Gesture}" Foreground="Red" />
with an accuracy of
<TextBlock Text="{Binding Accuracy}" />
</TextBlock>
私はあなたが意味することはこのようなものだと思います(テキストブロックのスタイリングではありません):
<TextBlock FontSize="25" >
<Run Text="Detected [" Foreground="Red"/><Run Text="gesture" Foreground="Gray"/>
<Run Text="] with an accuracy of [" Foreground="Yellow"/><Run Text="accuracy]" Foreground="Green"/>
</TextBlock>
注:Runタグ間のすべてのEnter(または改行)は、それらの間にスペースを作ります。
この投稿が古いことは知っていますが、これを試しましたか?この方法でTextBlockにマルチカラーテキストを実際に追加できます。
Xaml: <TextBlock x:Name="txt_Txt"/>
foreach (var itm5 in "! Hello World !; %Hello World%".Split(';'))
{
if (txt_Txt.Inlines.Count() > 0)
txt_Txt.Inlines.Add(new Run("\r\n"));
foreach (var letter in itm5)
{
if (char.IsSymbol(letter))
txt_Txt.Inlines.Add(new Run(letter.ToString()) { Foreground = Brushes.Red });
else
txt_Txt.Inlines.Add(new Run(letter.ToString()) { Foreground = Brushes.Black });
}
}
そのためにRichTextBoxを使用して設定できますIsReadOnly = true