25

how to get each character of a textblock of different color in wpf?

chetan
  • 361
  • 2
  • 4
  • 5

1 Answers1

62

Use many different runs:

 <TextBlock FontSize="22">
            <Run Foreground="Gold">H</Run>
            <Run Foreground="Maroon">e</Run>
            <Run Foreground="Blue">l</Run>
            <Run Foreground="Orange">l</Run>
            <Run Foreground="Brown">o</Run>
        </TextBlock>

This produce the result: enter image description here

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • 2
    How can I do this with MVVM? – Mateusz Dembski Jul 16 '13 at 12:48
  • Bind `TextBlock` text to a list of `Runs`? – wingerse Oct 16 '15 at 10:12
  • 1
    @EmpereurAiman You can't bind `List` to `Text` property of `TextBlock`. But you can bind to `Text` value of individual `Run`. Try: [http://stackoverflow.com/questions/2705444/can-i-have-multiple-colors-in-a-single-textblock-in-wpf](http://stackoverflow.com/questions/2705444/can-i-have-multiple-colors-in-a-single-textblock-in-wpf) – SachiraChin Jan 06 '16 at 02:34
  • 1
    @EmpereurAiman After small research, found way to bind. Please refer this. [http://stackoverflow.com/questions/1959856/data-binding-the-textblock-inlines#9546372](http://stackoverflow.com/questions/1959856/data-binding-the-textblock-inlines#9546372) – SachiraChin Jan 06 '16 at 02:47