从C#代码中设置WPF文本框边框颜色

20

我有一个WPF应用程序,其中需要进行多个更新。

其中之一的更新是我要从Label更改为TextBox

我看到在许多设置文本框边框颜色的XAML示例中,那对我来说行不通,因为存在业务规则条件要求红色或黑色。

我已经尝试过:

lblValidMsg.BorderBrush = Brushes.Red;
lblValidMsg.BorderBrush = System.Drawing.Color.Red;     // converter.ConvertFromString("#FF0000"); //borderColor;


lblValidMsg.BorderBrush = SystemColors.Control;

private Color borderColor = Color.Gray;

我相信这很“简单”,但是持续不断的错误却像

Cannot implicitly convert type 'System.Drawing.Color' to 'System.Windows.Media.Brush'   

我知道我把文本框的名称留成标签名称了,因此以“lbl”开头。

更新:

我看到有人设置背景和前景,但那不是我需要做的方式。

textBox1.Background = Brushes.Blue;
textBox1.Foreground = Brushes.Yellow;

我确实尝试过

lblValidMsg.BorderBrush = Brushes.Red;

这会导致不能隐式地将类型“System.Drawing.Color”转换为“System.Windows.Media.Brush”


可能会有帮助:将系统颜色设置为按钮 或者 设置文本框的背景颜色 - marbel82
1个回答

39
textBox.BorderBrush = System.Windows.Media.Brushes.Red;

对我有效,确保您没有使用System.Drawing.Brushes,您需要改用Windows.Media brush。


是的,那可以运行,我想信息有些显而易见,但你做到了!谢谢! - user5154099

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接