WPF TextBlock Color for each Character
Asked Answered
S

1

27

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

Superannuated answered 4/4, 2011 at 7:21 Comment(0)
D
64

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

Driest answered 4/4, 2011 at 7:28 Comment(4)
How can I do this with MVVM?Liveried
Bind TextBlock text to a list of Runs?Gestation
@EmpereurAiman You can't bind List<Run> to Text property of TextBlock. But you can bind to Text value of individual Run. Try: https://mcmap.net/q/505522/-can-i-have-multiple-colors-in-a-single-textblock-in-wpfDominion
@EmpereurAiman After small research, found way to bind. Please refer this. https://mcmap.net/q/401776/-data-binding-the-textblock-inlines#9546372Dominion

© 2022 - 2024 — McMap. All rights reserved.