【问题标题】:WPF set Textbox Border color from C# codeWPF 从 C# 代码设置文本框边框颜色
【发布时间】:2016-03-14 03:03:04
【问题描述】:

我有一个 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”

【问题讨论】:

标签: c# wpf xaml


【解决方案1】:
textBox.BorderBrush = System.Windows.Media.Brushes.Red;

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

【讨论】:

  • 是的,这行得通,我猜这个信息很明显,但你做到了!谢谢!
猜你喜欢
  • 2010-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
相关资源
最近更新 更多