我是 WPF 的新手。
目前我正在为名为“LabeledTextbox”的表单元素制作一个用户控件,其中包含一个标签、一个文本框和一个用于错误消息的文本块。
当使用代码添加错误消息时,我想将文本框的边框设为红色。但是,当错误消息被删除时,我想回到文本框的默认边框颜色。我觉得必须有一个非常简单的方法来做到这一点。
我的代码:
(在公共部分类 LabeledTextbox 中:UserControl)
public string ErrorMessage
{
set
{
if (string.IsNullOrEmpty(value))
{
_textbox.BorderBrush = Brushes.Black; //How do I revert to the original color in the most elegant way?
}
else
{
_textbox.BorderBrush = Brushes.Red;
}
_errorMessage.Text = value;
}
}