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

Bri*_*ton 12 c# wpf xaml

我有一个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;
Run Code Online (Sandbox Code Playgroud)

我确信它是"简单的",但不断的不同错误就像

Cannot implicitly convert type 'System.Drawing.Color' to 'System.Windows.Media.Brush'   
Run Code Online (Sandbox Code Playgroud)

是的我知道我将文本框名称作为标签名称,因此以"lbl"开头

更新:

我看到人们设置背景和前景,但这不是我需要做的

textBox1.Background = Brushes.Blue;
textBox1.Foreground = Brushes.Yellow;
Run Code Online (Sandbox Code Playgroud)

我确实试过了

lblValidMsg.BorderBrush = Brushes.Red;
Run Code Online (Sandbox Code Playgroud)

这样就不能隐式地将类型'System.Drawing.Color'转换为'System.Windows.Media.Brush'

小智 26

textBox.BorderBrush = System.Windows.Media.Brushes.Red;
Run Code Online (Sandbox Code Playgroud)

适合我,确保你没有使用System.Drawing.Brushes,你需要使用Windows.Media画笔.

  • 使用VS2017 CE对我不起作用 (4认同)