如何在 MVVM 中将背景颜色重置为默认值? [英] How to reset Background color to default in MVVM?

查看:25
本文介绍了如何在 MVVM 中将背景颜色重置为默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MVVM 在 UWP 中创建登录页面.当输入不正确的密码时,我希望将 PasswordBox 的边框更改为红色以表示它不正确.

I am creating a login page in UWP using MVVM. When an incorrect password is put in I want the border of the PasswordBox to be changed to red to indicate it is incorrect.

我将 SolidColorBrush 变量绑定到 Border 和 Background.

I bound a SolidColorBrush variable to the Border and Background.

        private SolidColorBrush _validationColorBorder;
        public SolidColorBrush ValidationColorBorder
        {
            get{ return _validationColorBorder; }
            set
            {
                _validationColorBorder = value;
                RaisePropertyChanged();
            }
        }

        private SolidColorBrush _validationColorBackground;
        public SolidColorBrush ValidationColorBackground
        {
            get { return _validationColorBackground; }
            set
            {
                _validationColorBackground = value;
                RaisePropertyChanged();
            }
        }

在 ViewModel 中,我使用以下方法将颜色设置为验证颜色:

in the ViewModel I set the colors to the validation Colors using this:

            ValidationColorBackground = (SolidColorBrush)Application.Current.Resources["TextBoxBackgroundThemeBrush"];
            ValidationColorBorder = (SolidColorBrush)Application.Current.Resources["TextBoxBorderThemeBrush"];

我的问题是,在将密码框背景和边框设置为这些颜色后,我希望之后能够将它们设置回默认颜色.

My issue is that after I set the password box background and border to these colors, I want to be able to set them back to the default colors afterwards.

我将如何将颜色设置回应用程序的默认颜色?并且能够通过将我的 SolidColorBrush 变量设置为这些默认颜色以 MVVM 格式执行此操作吗?

How would I go about setting the colors back to the default colors of my application? And be able to do this in the MVVM format by setting my SolidColorBrush variables to these default colors?

感谢您的帮助!

推荐答案

如何在 MVVM 中将背景颜色重置为默认值?

How to reset Background color to default in MVVM?

请检查 PasswrodBox 样式.默认的背景和边框颜色是 TextControlBackgroundTextControlBorderBrush.因此,您可以在设置自定义颜色之前获取它们.

Please check PasswrodBox style. The default Background and Border color are TextControlBackground and TextControlBorderBrush. So you could get them before you set the custom color.

defaultBgColor = (SolidColorBrush)Application.Current.Resources["TextControlBackground"];
defaultBorderColor = (SolidColorBrush)Application.Current.Resources["TextControlBorderBrush"];

如果你想回滚,你只需要将上面的值复制到你的自定义绑定属性中.更多内容请参考以下内容.

If you want to rollback,you just need to copy above value to your custom binding property. For more please refer the following.

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private SolidColorBrush defaultBgColor;
    private SolidColorBrush defaultBorderColor;

    public ViewModel()
    {
        defaultBgColor = (SolidColorBrush)Application.Current.Resources["TextControlBackground"];
        defaultBorderColor = (SolidColorBrush)Application.Current.Resources["TextControlBorderBrush"];

        ValidationColorBackground = (SolidColorBrush)Application.Current.Resources["TextBoxBackgroundThemeBrush"];
        ValidationColorBorder = (SolidColorBrush)Application.Current.Resources["TextBoxBorderThemeBrush"];
    }
    private SolidColorBrush _validationColorBorder;
    public SolidColorBrush ValidationColorBorder
    {
        get { return _validationColorBorder; }
        set
        {
            _validationColorBorder = value;
            RaisePropertyChanged();
        }
    }

    private void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private SolidColorBrush _validationColorBackground;
    public SolidColorBrush ValidationColorBackground
    {
        get { return _validationColorBackground; }
        set
        {
            _validationColorBackground = value;
            RaisePropertyChanged();
        }
    }

    public ICommand BtnClickCommand
    {
        get
        {
            return new RelayCommand(() =>
            {
                ValidationColorBackground = defaultBgColor;
                ValidationColorBorder = defaultBorderColor;
            });
        }
    }

}

这篇关于如何在 MVVM 中将背景颜色重置为默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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