Change color in part of text in textblock

不想你离开。 提交于 2021-01-27 06:22:41

问题


I need change color in code behind for part of text string..

My exaple:

textblock1.Text = string1 + string2 + string3;

String have dynamic values, and i want to string2 display after running the program in blue color and it must be defined in the code behind.

Its possible? Thank you!


回答1:


That working

                        textblock1.Inlines.Clear();
                        textblock1.Inlines.Add(new Run(string1));
                        textblock1.Inlines.Add(new Run(string2) { Foreground = Brushes.Blue });



回答2:


I Hope that will help you:

<TextBlock FontSize="16">
        <Run Foreground="Red">Your_Text_1</Run>
        <Run Foreground="Orange">Your_Text_2</Run>
        <Run Foreground="purple">Your_Text_3</Run>
</TextBlock>



回答3:


You can use "Run" Ex : "Run Name="Value_from_codebehind" />"

In the code behind attach the value



来源:https://stackoverflow.com/questions/26907164/change-color-in-part-of-text-in-textblock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!