如果模型中的验证出现错误,则禁用按钮

发布于 2024-10-16 01:21:27 字数 3079 浏览 1 评论 0原文

您好,我对模型类中的错误进行验证。

 public class CurrentUser:IDataErrorInfo, INotifyPropertyChanged
    {
//...

        private string _validationResult;
        private string _nick;

        public string Nick
        {
            get { return _nick; }
            set
            {
                _nick = value;
                NotifyPropertyChanged("Nick");
            }
        }

        public string ValidationResult
        {
            get { return _validationResult; }
            private set
            {
                _validationResult = value;
                NotifyPropertyChanged("ValidationResult");
            }
        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion

        #region Implementation of IDataErrorInfo

        private string NickValid()
        {
            if (string.IsNullOrEmpty(Nick))
            {
                return NickNull;
            }
            if (Regex.IsMatch(Nick, "[^a-zA-Z0-9-_.]"))
            {
                return NickInvalidCharacters;
            }
            return string.Empty;
        }

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        public string this[string propertyName]
        {

            get
            {
                ValidationResult = string.Empty;

                switch (propertyName)
                {
                    case "Nick":
                        ValidationResult = NickValid();
                        break;
                    default:
                        break;
                }
                return ValidationResult;

            }
        }

        #endregion

    }

我在视图模型中使用这个模型类,并将模型类的 Nick 属性绑定到组合框控件的 Text 属性。

此外,我还在视图中的按钮单击事件上绑定了视图模型类中的 LogOn 方法。如果模型类中的验证有错误,我想要禁用按钮:

查看模型:

[Export(typeof(ILogOnViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class LogOnViewModel : Screen, ILogOnViewModel,
    IPartImportsSatisfiedNotification
{

    public CurrentUser CurrentUser { get; set; }

    public bool CanLogOn
    {
        get
        {
            return string.IsNullOrWhiteSpace(CurrentUser.ValidationResult);
        }
    }

     //bind on button click event
    public void LogOn()
    {}
 }

解决方案很简单,如果 CurrentUser(对象)属性中的验证有错误,则将 CanLogOn 属性设置为 false。

但我不知道如何通知模型类中的属性 CanLogOn 没有错误。我运行应用程序,按钮仍然处于禁用状态。

我需要在模型中实现这种行为:

    public string ValidationResult
    {
        get { return _validationResult; }
        private set
        {
            _validationResult = value;
            NotifyPropertyChanged("ValidationResult");
            //notify property CanLogOn in view model class
        }
    }

有什么建议吗?感谢。

Hi I make validation on error in my model class.

 public class CurrentUser:IDataErrorInfo, INotifyPropertyChanged
    {
//...

        private string _validationResult;
        private string _nick;

        public string Nick
        {
            get { return _nick; }
            set
            {
                _nick = value;
                NotifyPropertyChanged("Nick");
            }
        }

        public string ValidationResult
        {
            get { return _validationResult; }
            private set
            {
                _validationResult = value;
                NotifyPropertyChanged("ValidationResult");
            }
        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion

        #region Implementation of IDataErrorInfo

        private string NickValid()
        {
            if (string.IsNullOrEmpty(Nick))
            {
                return NickNull;
            }
            if (Regex.IsMatch(Nick, "[^a-zA-Z0-9-_.]"))
            {
                return NickInvalidCharacters;
            }
            return string.Empty;
        }

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        public string this[string propertyName]
        {

            get
            {
                ValidationResult = string.Empty;

                switch (propertyName)
                {
                    case "Nick":
                        ValidationResult = NickValid();
                        break;
                    default:
                        break;
                }
                return ValidationResult;

            }
        }

        #endregion

    }

This model class I use in view model and I bind Nick property of model class to the Text property of comboBox control.

Also I bind method LogOn from view model class on button click event in view. I would like disabale button if validation in model class has error:

View model:

[Export(typeof(ILogOnViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class LogOnViewModel : Screen, ILogOnViewModel,
    IPartImportsSatisfiedNotification
{

    public CurrentUser CurrentUser { get; set; }

    public bool CanLogOn
    {
        get
        {
            return string.IsNullOrWhiteSpace(CurrentUser.ValidationResult);
        }
    }

     //bind on button click event
    public void LogOn()
    {}
 }

Solution is simple set CanLogOn property on false if validation in CurrentUser (object) property has error.

But I don’t how notify property CanLogOn that in model class is not error. I run app and button is still disabled.

I need achive this behavior in model:

    public string ValidationResult
    {
        get { return _validationResult; }
        private set
        {
            _validationResult = value;
            NotifyPropertyChanged("ValidationResult");
            //notify property CanLogOn in view model class
        }
    }

Any advice? Thank.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不羁少年 2024-10-23 01:21:27

将事件处理程序附加到视图模型中用户的 PropertyChanged 事件:

CurrentUser.PropertyChanged += new PropertyChangedEventHandler(CurrentUser_PropertyChanged);

添加如果 ValidationResult 更改则发送通知:

void CurrentUser_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "ValidationResult") NotifyPropertyChanged("CanLogOn");
}

注意:如果您的引用CurrentUser 被覆盖,您需要将事件处理程序添加到新对象。您可以通过将附件代码放置在 CurrentUser 的 setter 中来完成此操作。

Attach an event handler to the PropertyChanged event of the user in your viewmodel:

CurrentUser.PropertyChanged += new PropertyChangedEventHandler(CurrentUser_PropertyChanged);

Add send a notification if the ValidationResult changes:

void CurrentUser_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "ValidationResult") NotifyPropertyChanged("CanLogOn");
}

Note: If your reference CurrentUser is overwritten you need to add the event handler to the new object. You could do this by placing the attachment code in the setter of CurrentUser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文