Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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# 在WPF应用程序中以编程方式设置文本框的颜色_C#_Wpf_Colors_Background_Textbox - Fatal编程技术网

C# 在WPF应用程序中以编程方式设置文本框的颜色

C# 在WPF应用程序中以编程方式设置文本框的颜色,c#,wpf,colors,background,textbox,C#,Wpf,Colors,Background,Textbox,在试图设置WPF应用程序中文本框的颜色时,我做到了 为什么我会犯这样的错误 如何纠正呢 textBox1.Background = Brushes.Blue; textBox1.Foreground = Brushes.Yellow; 你可以这样做 你可以这样做。它告诉你错误是正确的。Background属性的类型为System.Windows.Media.Brush,而不是System.Windows.Media.Color,因此无法为其分配颜色 将画笔而不是颜色传递到方法中,并将其指定给

在试图设置WPF应用程序中文本框的颜色时,我做到了

为什么我会犯这样的错误

如何纠正呢

textBox1.Background = Brushes.Blue;
textBox1.Foreground = Brushes.Yellow;
你可以这样做


你可以这样做。

它告诉你错误是正确的。
Background
属性的类型为
System.Windows.Media.Brush
,而不是
System.Windows.Media.Color
,因此无法为其分配
颜色

画笔
而不是
颜色
传递到方法中,并将其指定给
背景
属性

public void addToStackPanel(string argBuiltAlarm, Brush brush)
{
    ...
    TextBox textBox = new TextBox { Background = brush };
    ...

它告诉你错误是正确的。
Background
属性的类型为
System.Windows.Media.Brush
,而不是
System.Windows.Media.Color
,因此无法为其分配
颜色

画笔
而不是
颜色
传递到方法中,并将其指定给
背景
属性

public void addToStackPanel(string argBuiltAlarm, Brush brush)
{
    ...
    TextBox textBox = new TextBox { Background = brush };
    ...

或者,可以从给定颜色创建笔刷:
Brush Brush=new solidcolorbush(color)
Solved.Thank.@elgonzo,你也完全正确(我使用了你的解决方案)。或者,可以从给定颜色创建笔刷:
Brush Brush=new solidcolorbush(color)
Solved.Thank.@elgonzo,你也完全正确(我使用了你的解决方案)