我是WPF的一名完整新手。WPF:恢复笔刷为默认/原创
目前我正在为名为“LabeledTextbox”的表单元素进行一个用户控件,该表单包含一个标签,一个文本框和一个用于errormessages的文本块。
当使用代码添加errormessage时,我想将文本框的边框置为红色。但是,当errormessage被删除时,我想回到文本框的默认bordercolor。 我觉得必须有一个非常简单的方法来做到这一点。
我的代码:
(公共部分类LabeledTextbox:用户控件)
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;
}
}
太好了,谢谢! *将dependencyproperty研究添加到待办事项列表* – 2009-08-20 14:25:30
非常感谢,非常有用的文章。我尝试使用brush.clone方法存储默认的笔刷,但是根据.net,系统默认不存在笔刷。谢谢! – JustinKaz 2010-05-27 16:34:33
这对我不起作用。当我想要引起注意时,我将背景设置为海蓝宝石。我尝试使用ClearValue将其还原回来,但仍保持为海蓝宝石。看看这篇文章:(http://wpf.2000things.com/tag/clearvalue/)告诉我,ClearValue将它恢复到最后指定的值。所以我试着将背景属性设置为Nothing,它工作。标签背景现在是默认颜色。 – tolsen64 2015-04-09 17:27:04