Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改textblock中部分文本的颜色_C#_Wpf_String_Colors_Code Behind - Fatal编程技术网

C# 更改textblock中部分文本的颜色

C# 更改textblock中部分文本的颜色,c#,wpf,string,colors,code-behind,C#,Wpf,String,Colors,Code Behind,我需要为部分文本字符串更改代码中的颜色 我举个例子: textblock1.Text = string1 + string2 + string3; 字符串具有动态值,我希望在以蓝色运行程序后显示string2,并且它必须在代码中定义 有可能吗?谢谢大家! 我希望这将对您有所帮助: <TextBlock FontSize="16"> <Run Foreground="Red">Your_Text_1</Run> <Run

我需要为部分文本字符串更改代码中的颜色

我举个例子:

textblock1.Text = string1 + string2 + string3;
字符串具有动态值,我希望在以蓝色运行程序后显示string2,并且它必须在代码中定义


有可能吗?谢谢大家!

我希望这将对您有所帮助:

<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>

你的文字
你的文字
你的文字
那是工作吗

                        textblock1.Inlines.Clear();
                        textblock1.Inlines.Add(new Run(string1));
                        textblock1.Inlines.Add(new Run(string2) { Foreground = Brushes.Blue });
你可以用“跑” 例如:“运行Name=“Value\u from\u codebeahind”/>”


在代码隐藏中附加值

谢谢您的回复,但我需要在代码隐藏中定义它,因为textblock不显示静态文本,但不同数量的变量具有不同的值:/请参考以下答案: