WPF:将画笔还原为默认/原始 [英] WPF: reverting brush to default/original

查看:91
本文介绍了WPF:将画笔还原为默认/原始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF的一名新手。



此刻,我正在为名为 LabeledTextbox的表单元素制作用户控件,其中包含标签,文本框和用于错误消息的文本块。



当使用代码添加错误消息时,我想将文本框的边框设置为红色。但是,当错误消息被删除后,我想回到文本框的默认边框颜色。
我觉得必须有一个非常简单的方法来做到这一点。



我的代码:



(在公共局部类LabeledTextbox中:UserControl)

 公共字符串ErrorMessage 
{
set
{
if(string.IsNullOrEmpty(value))
{
_textbox.BorderBrush = Brushes.Black; //如何以最优雅的方式还原为原始颜色?
}
其他
{
_textbox.BorderBrush = Brushes.Red;
}

_errorMessage.Text =值;
}
}


解决方案

您可以使用

  _textBox.ClearValue(TextBox.BorderBrushProperty); 

这将删除直接分配的值,并返回到样式或模板定义的值。 / p>

I'm a complete newbie at WPF.

At the moment I'm making a usercontrol for form elements called "LabeledTextbox" which contains a label, a textbox and a textblock for errormessages.

When the using code adds an errormessage, I want to put the border of the textbox in red. But, when the errormessage gets removed, I'd like to turn back to the default bordercolor of the textbox. I feel there must be a very easy way to do this.

My code:

(in public partial class 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;
    }
}

解决方案

You could use

_textBox.ClearValue(TextBox.BorderBrushProperty);

That will remove the directly assigned value and go back to the value defined by the style or template.

这篇关于WPF:将画笔还原为默认/原始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆