Can I have multiple colors in a single TextBlock in WPF?

前端 未结 4 2003
小鲜肉
小鲜肉 2020-12-19 01:54

I have a line of text in a textblock that reads:

\"Detected [gesture] with an accuracy of [accuracy]\"

In WPF, is it possible for me to be able to change th

相关标签:
4条回答
  • 2020-12-19 02:41

    I know this post is old, but have you tried this?? You can actually add multi colored text, this way in a 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 });
            }
    }
    
    0 讨论(0)
  • 2020-12-19 02:45

    you can use a RichTextBox for that and set IsReadOnly = true

    0 讨论(0)
  • 2020-12-19 02:52

    I think what you mean is like this( not styling for 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>
    

    Note: every enter(or new line) between Run tag make have space between them.

    0 讨论(0)
  • 2020-12-19 02:54

    See if this helps:

     <TextBlock>
          Detected
          <TextBlock Text="{Binding Gesture}" Foreground="Red" />
          with an accuracy of
          <TextBlock Text="{Binding Accuracy}" />
     </TextBlock>
    
    0 讨论(0)
提交回复
热议问题