MVVM中的数据验证

3

我有一个带有多个ViewModel的应用程序。有些属性有DataAnnotations。

 [Required(ErrorMessage = "Field 'Range' is required.")]
    [Range(1, 10, ErrorMessage = "Field 'Range' is out of range.")]
    public int Password
    {
        get
        {
            return _password;
        }
        set
        {
            if (_password != value)
            {
                _password = value;
                RaisePropertyChanged("Password");
            }
        }
    }

如何通过实现IDataErrorInfo或INotifyDataErrorInfo接口对所有视图模型实现验证?

我使用了这篇文章,在属性更改时进行验证,但不验证必填字段。

2个回答

2
这里有一个使用IDataErrorInfo的简单示例,可以帮助您入门。
XAML:
<Grid>
    <Grid.Resources>
        <ControlTemplate x:Key="LeftErrorTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding AdornedElement.(Validation.Errors).[0].ErrorContent, ElementName=ErrorAdorner}" Background="Red" Foreground="White" FontWeight="Bold" VerticalAlignment="Center"/>
                <AdornedElementPlaceholder x:Name="ErrorAdorner">
                    <Border BorderBrush="Red" BorderThickness="1" />
                </AdornedElementPlaceholder>
            </StackPanel>
        </ControlTemplate>
    </Grid.Resources>
    <TextBlock Height="23" HorizontalAlignment="Left" Margin="158,66,0,0" Name="textBlock1" Text="Name" VerticalAlignment="Top" Width="44" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="217,65,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" 
             Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" 
             Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}"/>
</Grid>

代码后端:

using System;
using System.Windows;
using System.ComponentModel;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var vm = new ViewModel();

            this.DataContext = vm;
        }
    }

    public class ViewModel : ObservableBase, IDataErrorInfo
    {
        private string _Name;

        public string Name
        {
            get { return _Name; }
            set
            {
                _Name = value;
                OnPropertyChanged("Name");
            }
        }

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

        public string this[string columnName]
        {
            get
            {
                string errorMessage = string.Empty;

                switch (columnName)
                {
                    case "Name":
                        if (string.IsNullOrEmpty(Name))
                            errorMessage = "Enter name";
                        else if (Name.Trim() == string.Empty)
                            errorMessage = "Enter valid name";
                        break;
                }
                return errorMessage;
            }
        }

    }

    public class ObservableBase : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

谢谢,但我想为整个应用程序创建一个验证类,并让所有的ViewModel都继承自验证类。 - ar.gorgin

1

你需要创建一些方法来验证注解,就像我的代码中的ValidateMethod。

public class ViewModel:INotifyPropertyChanged,IDataErrorInfo
    {
        int _password;

        [Required(ErrorMessage = "Field 'Range' is required.")]
        [Range(1, 10, ErrorMessage = "Field 'Range' is out of range.")]
        public int Password
        {
            get
            {
                return _password;
            }
            set
            {
                if (_password != value)
                {
                    _password = value;
                    Validate("Password", value);
                    Notify("Password");
                }
            }
        }

        private void Validate(string propertyName, object value)
        {
            if (string.IsNullOrEmpty(propertyName))
                throw new ArgumentNullException("propertyName");

            string error = string.Empty;

            var results = new List<System.ComponentModel.DataAnnotations.ValidationResult>(2);

            bool result = Validator.TryValidateProperty(
                value,
                new ValidationContext(this, null, null)
                {
                    MemberName = propertyName
                },
                results);

            if (!result && (value == null || ((value is int || value is long) && (int)value == 0) || (value is decimal && (decimal)value == 0)))
                return;

            if (!result)
            {
                System.ComponentModel.DataAnnotations.ValidationResult validationResult = results.First();
                if (!errorMessages.ContainsKey(propertyName))
                    errorMessages.Add(propertyName, validationResult.ErrorMessage);
            }

            else if (errorMessages.ContainsKey(propertyName))
                errorMessages.Remove(propertyName);
        }

        #region INotifyPropertyChanged 

        public void Notify(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));

        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDataErrorInfo

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

        private Dictionary<string, string> errorMessages = new Dictionary<string, string>();

        public string this[string columnName]
        {
            get 
            { 
                if(errorMessages.ContainsKey(columnName))
                    return errorMessages[columnName];
                return null;

            }
        }

        #endregion
    }
<TextBox Text="{Binding Password, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" Height="70" Width="200" />

xaml.cs

public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }

你需要在应用DataAnnotations的属性的setter方法中调用Validate方法,并确保在通知PropertyChanged之前进行调用。

谢谢,但是 PropertyChanged 在必要的验证中没有通知,我应该在提交按钮中为所有必需的属性调用验证方法。 - ar.gorgin
你需要为所有要验证的属性调用该方法,而PropertyChanged不会在必需的验证中发出通知。 - yo chauhan
只有在绑定发生时才会触发验证。当用户启动屏幕并直接点击提交按钮时会发生什么...没有绑定发生,因此不会触发任何验证.. - ar.gorgin
你可以看到有一个字典(errorMessages),其中包含错误消息,你可以使用它。如果字典的计数大于1,则保持按钮禁用状态。如果你想要通知,只需为字典键中的每个属性调用RaisePropertyChanged即可。 - yo chauhan
那就取决于你的逻辑,你是怎么实现它的。为此,你需要将命令与按钮绑定。我现在没有代码,因为我不在我的笔记本电脑上。 - yo chauhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接