wpf 更改文本块中部分文本的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26907164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:41:55  来源:igfitidea点击:

Change color in part of text in textblock

c#wpfstringcolorscode-behind

提问by Honza Kalina

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.

字符串具有动态值,我想在运行程序后以蓝色显示字符串2,并且必须在后面的代码中定义它。

Its possible? Thank you!

这是可能的?谢谢!

回答by Honza Kalina

That working

那个工作

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

回答by Dotnetter

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>

回答by RJV

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

您可以使用“运行”例如: “运行名称=“Value_from_codebehind”/>”

In the code behind attach the value

在后面的代码中附加值