2

I am trying to make each line of text on a WPF textblock display in a different color. I have the following code which makes the entire block's font color purple because that's the last color it's set to. How can I make it so each potion is displayed in a different color?

    private void btnShowPotions_Click(object sender, RoutedEventArgs e) {

        tbPotionInfo.Foreground = Brushes.Green;
        tbPotionInfo.Text = smallPotion.Name + "(" + smallPotion.AffectValue + ")\r\n";
        tbPotionInfo.Foreground = Brushes.Blue;
        tbPotionInfo.Text += mediumPotion.Name + "(" + mediumPotion.AffectValue + ")\r\n";
        tbPotionInfo.Foreground = Brushes.Red;
        tbPotionInfo.Text += largePotion.Name + "(" + largePotion.AffectValue + ")\r\n";
        tbPotionInfo.Foreground = Brushes.Purple;
        tbPotionInfo.Text += extremePotion.Name + "(" + extremePotion.AffectValue + ")\r\n";
    }
Taterhead
  • 5,763
  • 4
  • 31
  • 40
kalenpw
  • 695
  • 3
  • 10
  • 34
  • 1
    you can make use of `Run` here – Gopichandar Mar 04 '16 at 06:59
  • Take a look at this http://www.wpf-tutorial.com/basic-controls/the-textblock-control-inline-formatting/ - specifically the section named RUN and SPAN. – Marek M. Mar 04 '16 at 06:59
  • use richtextbox and set it to readonly , or you have to setup textblock with one textbox for each color – Thorarins Mar 04 '16 at 07:00
  • @Thorarins you're wrong - with the usage of `Run`s or `Span`s he will be working with one `TextBlock` only. Besides - `RichTextBox` is reaaaaally slow and laggy. Have you actually tried to use it in your apps? – Marek M. Mar 04 '16 at 07:02
  • @Gopichandar I saw runs in a similar and question and am not sure how to implement them because the text isn't generated until the button is clicked my Xaml doesn't have any runs to edit. – kalenpw Mar 04 '16 at 07:03
  • Then add them in your code behind. – Marek M. Mar 04 '16 at 07:04
  • @SzwornyDziach Thank you. For completeness sake if you post that as an answer I'll accept it. I used the syntax here for adding runs programmatically http://stackoverflow.com/questions/10906539/how-to-assign-a-run-to-a-text-property-programmatically – kalenpw Mar 04 '16 at 07:06
  • 1
    @Gopichandar is already ahead of me ;) – Marek M. Mar 04 '16 at 07:07
  • @SzwornyDziąch yes richtextbox is a bit slow. but run is messy and makes the code hard to maintain – Thorarins Mar 04 '16 at 07:10

2 Answers2

9

You can make use of Run.

Here is the sample of how to use the Run

Run run = new Run(smallPotion.Name + "(" + smallPotion.AffectValue + ")\r\n");
run.Foreground = Brushes.Green;
tbPotionInfo.Inlines.Add(run);   

run = new Run(mediumPotion.Name + "(" + mediumPotion.AffectValue + ")\r\n");
run.Foreground = Brushes.Blue;
tbPotionInfo.Inlines.Add(run);        
...

Haven't verified but I hope it will help you.

Gopichandar
  • 2,742
  • 2
  • 24
  • 54
4

Use textblock like this

<TextBlock>
      <TextBlock Name="tbSmallPotion" Foreground="Green"/
      <TextBlock Text="tbMediumPotion"Foreground="Blue"/>
 </TextBlock>

and set the values

tbSmallPotion.Text = smallPotion.Name + "(" + smallPotion.AffectValue + ")\r\n";
tbMediumPotion.Text = mediumPotion.Name + "(" + mediumPotion.AffectValue + ")\r\n";
Thorarins
  • 1,836
  • 16
  • 21
  • Yes this would work, I ended up going with Gopichandar's solution for the simplicity of having one textblock. – kalenpw Mar 04 '16 at 07:09