wpf - 使用 IDataErrorInfo 和更新按钮状态在验证期间启用禁用保存按钮

标签 wpf button mvvm idataerrorinfo

我是 WPF MVVM 的新手,想对本文提出一个后续问题:
Enable Disable save button during Validation using IDataErrorInfo
如果表单验证上的许多控件中的任何一个失败/通过,我正在尝试启用/禁用按钮保存/更新。
我有 IsValid 方法,它检查模型上的验证逻辑并返回 True/False,它将作为谓词传递给 DelegateCommand。
问题是:我的按钮具有以下属性 IsEnabled{binding IsValid},这应该检查所有字段以确保它与模型中的条件匹配,向 View 模型返回 true/false,然后在全部为 true 时启用按钮。问题是:一旦 View 模型被实例化,就会在 处创建带有验证 (IsValid) 的 DelegateCommand 对象。假 即使用户在文本框中填充数据,它也会在对象的整个生命周期中保持这种状态。满足所有条件后如何打开按钮?换句话说,如何不断验证和更新 IsValid 以便在每个文本框验证为 true 时打开按钮?
谢谢,
我有以下代码:
型号

public class UserModel : ObservePropertyChanged,IDataErrorInfo
    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; OnPropertyChanged("Name"); }
        }
        // if there is an error throw an exception
        public string Error
        {
            get { throw new NotImplementedException(); }
        }
        public string this[string columnName]
        {
            get
            {
                string result = null;
                if (columnName == "Name")
                {
                    if (string.IsNullOrEmpty(Name))
                        result = "Please enter a Name";
                }
                return result;
            }
        }

        // the Josh Way
        static readonly string[] ValidatedProperties =
        {
            "Name"
        };

        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                {

                    if (GetValidationError(property) != null) // there is an error
                        return false;
                }
                return true;
            }
        }

        // a method that checks validation error
        private string GetValidationError(string propertyName)
        {
            string error = null;

            switch (propertyName)
            {
                case "Name":
                    error = this.ValidateName();
                    break;

                default:
                    error = null;
                    throw new Exception("Unexpected property being validated on Service");
            }
            return error;
        }
        private string ValidateName()
        {
            string ErrorMsg = null;
            if (string.IsNullOrWhiteSpace(Name))
            {
                ErrorMsg = "Name can't be empty!";
            };
            return ErrorMsg;
        }
}
** View 模型 **
public class UserViewModel:ObservePropertyChanged
    {
        UserModel model;
        public UserViewModel()
        {
            presentCommand = new DelegateCommand(param => PresentDataMethod(), param => CanSave);
            model = new UserModel();
        }

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; OnPropertyChanged("Name"); }
        }
        private string info;

        public string Info
        {
            get { return info; }
            set { info = value; OnPropertyChanged("Info"); }
        }
        private DelegateCommand presentCommand;

        public DelegateCommand PresentCommand
        {
            get 
            {
                if (presentCommand==null)
                {
                    presentCommand = new DelegateCommand(param => PresentDataMethod(), param => CanSave);
                }
                return presentCommand; 
            }
        }
        private void PresentDataMethod() 
        {
            Info = $"Your Name is: {Name}.";
        }

        // The ViewModel then contains a CanSave Property that reads the IsValid property on the Model:
        protected bool CanSave
        {
            get
            {
                return model.IsValid;
            }
        }
    }
** 风景**
<TextBox x:Name="Name" HorizontalAlignment="Left" Height="34" Margin="285,145,0,0" TextWrapping="Wrap" 
                 VerticalAlignment="Top" Width="248">
            <Binding Path="Name" 
                     ValidatesOnDataErrors="True" 
                     UpdateSourceTrigger="PropertyChanged" 
                     Mode="TwoWay">
            </Binding>
        </TextBox>
        <Button Content="Present" FontSize="20" HorizontalAlignment="Left" 
                Margin="285,184,0,0" VerticalAlignment="Top" Width="248" Height="35"
                Command="{Binding Path=PresentCommand}"
                IsEnabled="{Binding IsValid}"
                >
        </Button>

最佳答案

如果您只想通过 IsValid 刷新按钮值,您所要做的就是监听 ViewModel 中的任何 OTHER 属性更改,当发生这种情况时,告诉它刷新 IsValid绑定(bind)(实际上是你的 CanSave 属性)。这是一种方法:
** View 模型 **

// ...

public UserViewModel()
{
    // ...
    this.PropertyChanged += OnViewModelPropertyChanged;
}

public void OnViewModelPropertyChanged(object sender, PropertyEventArgs e)
{
    // On any property that implements "OnPropertyChanged(propname)", refresh the CanSave binding too!
    OnPropertyChanged(nameof(this.CanSave));
}

// ...
顺便说一句,避免使用像 OnPropertyChanged("Name") 这样的魔法词通常是一种很好的编码习惯。或 OnPropertyChanged("Info")因为您永远不知道开发人员何时必须重命名他们的属性,如果发生这种情况,您将不会在此处收到编译错误,并且可能很难调试。最好使用 nameof ,如 OnPropertyChanged(nameof(Name))这样您就会知道,如果您决定将属性更改为 FirstName,就会出现编译错误。而不是 Name .

关于wpf - 使用 IDataErrorInfo 和更新按钮状态在验证期间启用禁用保存按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65292978/

相关文章:

wpf - 将图标放在 WPF 扩展器控件的右端

html - 如何不让文字覆盖按钮?

c# - 如何像图像一样在 C# WPF 窗口中添加 svg/xaml 文件?

javascript - 单击按钮时传单缩放至属性

swift - 如何在swift SKSpriteNode中添加按钮效果

c# - 如何使用 MVVM 在 WP7 中聚焦文本框?

c# - 我到底在用什么?

c# - 更改基本 MVVM 的当前实现以遵守 SOLID 模式

c# - 如何实现 WPF 功能区上下文选项卡 MVVM 方式

c# - WPF DatePicker 没有使用 Material Design 的样式