【问题标题】:Data validation on ShowDialog window in WPFWPF 中 ShowDialog 窗口上的数据验证
【发布时间】:2011-07-12 10:08:06
【问题描述】:

我有一个显示为 ShowDialog 的窗口 在窗口中,我有一些文本框绑定到实现 INotifyPropertyChanges 和 IDataErrorInfo 的对象。 我希望只有在所有文本框都验证后才会启用“确定”按钮 我希望只要用户点击确定按钮就会发生下一步动作。

我可以将按钮绑定到 ICommand 并检查 CanExcute() 中的文本框验证,但是我可以在 Excute 中做什么?对象不知道窗口。 我还可以检查文本框验证,然后引发所有有效的事件并启用“确定”按钮,但随后会出现重复代码,因为我已经在 IDataErrorInfo 实现中进行了检查。

那么正确的方法是什么?

提前致谢

【问题讨论】:

    标签: wpf validation idataerrorinfo showdialog


    【解决方案1】:

    CanExecute 应该如下所示。

       public bool CanExecuteOK        
       {
            get
            {
                  if (DataModelToValidate.Error == null && DataModelToValidate.Errors.Count == 0) return true;
                  else return false;
            }
       }
    

    这里的 Error 和 Errors 属性只不过是 this[string propertyName] 上的 Wrapper(为 IDataErrorInfo 隐式实现)。

    这是示例模型类:

        public class SampleModel: IDataErrorInfo, INotifyPropertyChanged
        {
            public SampleModel()
            {
                this.Errors = new System.Collections.ObjectModel.ObservableCollection<string>();
            }
            private string _SomeProperty = string.Empty;
            public string SomeProperty
            {
                get
                {
                    return _SomeProperty;
    
                }
                set
                {
                    if (value != _SomeProperty)
                    {
                        _SomeProperty= value;
                        RaisePropertyChanged("SomeProperty");
                    }
                }
            }
    ....
    ....
            //this keeps track of all errors in current data model object
            public System.Collections.ObjectModel.ObservableCollection<string> Errors { get; private set; }
            //Implicit for IDataErrorInfo
            public string Error
            {
                get
                {
                    return this[string.Empty];
                }
            }
    
            public string this[string propertyName]
            {
                get
                {
    
                    string result = string.Empty;
                    propertyName = propertyName ?? string.Empty;
    
                    if (propertyName == string.Empty || propertyName == "SomeProperty")
                    {
                        if (string.IsNullOrEmpty(this.SomeProperty))
                        {
                            result = "SomeProperty cannot be blank";
                            if (!this.Errors.Contains(result)) this.Errors.Add(result);
                        }
                        else
                        {
                            if (this.Errors.Contains("SomeProperty cannot be blank")) this.Errors.Remove("SomeProperty cannot be blank");
                        }
                    }
    ......
          return result;
        }
    

    【讨论】:

    • 感谢您的回答!但是如何启用按钮以及在 ExecuteOk 中可以做什么? SampleModel 不知道窗口。
    • CanExecuteOK 将为您做到这一点。你不必担心。如果 CanExecuteOK 返回 false,您的按钮将被自动禁用,如果为 true,它将被启用。回想一下,对于命令绑定,您需要实现 2 个方法。可以执行确定和执行确定。 CanExecute 会告诉何时启用/禁用,而 ExecuteOK 会做什么。
    猜你喜欢
    • 2010-09-26
    • 2021-05-05
    • 2015-05-29
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    相关资源
    最近更新 更多