0

在我的上一个问题中,我得到了一个解决方案,如何手动检查验证结果https://stackoverflow.com/questions/39031783/screen-validation-with-data-annotations-on-button-click-in-using-inotifydataerro/ 39033124#39033124

我有一个 Model Person,它包含三个属性FirstName(必需)、MiddleName(可选)和LastName(必需)。

现在我稍微改变了我的代码

public class PersonViewModel : BaseViewModel, INotifyDataErrorInfo {

    private string _personObj = string.Empty;

    public Person PersonObj
    {
        get { return _personObj; }
        set { _personObj= value; OnPropertyChanged(); }
    }

    public bool IsValidObject
    {
        get 
        { 
            var context = new ValidationContext(EmpInfo, null, null);
            bool flag = Validator.TryValidateObject(EmpInfo, context, null, true);
            return flag;
        }
    }

    public ICommand SaveDataCommand
        {
            get
            {
                return new DelegatingCommand(SaveData);
            }
        }

    public void SaveData
        {
            // Validate Data;
        }

}

模型类:人

public class Person
{
    [Required]
    public string FirstName { get; set; }

    public string MiddleName { get; set; }

    [Required]
    public string LastName { get; set; }
}

我的 XAML 是

<TextBox Text="{Binding PersonObj.FirstName, UpdateSourceTrigger=PropertyChanged,
 ,ValidatesOnNotifyDataErrors=True}" />

<TextBox Text="{Binding PersonObj.MiddleName, UpdateSourceTrigger=PropertyChanged,
 ,ValidatesOnNotifyDataErrors=True}" />

<TextBox Text="{Binding PersonObj.LastName, UpdateSourceTrigger=PropertyChanged,
 ,ValidatesOnNotifyDataErrors=True}" />

<Button Content="Save" IsDefault="True" IsEnabled="{Binding IsValidObject}" Command="{Binding SaveDataCommand}" />

该属性IsValidObject最初工作正常。如果我更新 and 中的值FirstNameLastName则该属性IsValidObject不会更新 UI 中的结果。

我使用了另一种方法

public class PersonViewModel : BaseViewModel, INotifyDataErrorInfo {

    private string _personObj = string.Empty;

    public Person PersonObj
    {
        get { return _personObj; }
        set { _personObj= value; OnPropertyChanged(); }
    }

    public bool IsValidObject
    {
        get 
        { 
            var context = new ValidationContext(EmpInfo, null, null);
            bool flag = Validator.TryValidateObject(EmpInfo, context, null, true);
            return flag;
        }
    }

    public ICommand SaveDataCommand
        {
            get
            {
                return new DelegatingCommand(SaveData, ValidateEmployeeObject);
            }
        }


    public bool ValidateEmployeeObject()
    {
        var context = new ValidationContext(EmpInfo, null, null);
        bool flag = Validator.TryValidateObject(EmpInfo, context, ErrorResult, true);
        return flag;
    }

    public void SaveData
        {
            // Validate Data;
        }

}

这里我介绍了一个Method ValidateEmployeeObject(),它返回TRUE/FALSE作为类似于Property IsValidObject。我按照@adminSoftDK 在评论部分的建议将这个方法绑定在CanExecute块中。SaveDataCommand这种方法也失败了......

我如何根据上述验证更新保存按钮以启用。

4

1 回答 1

0

如果你尝试这样的事情

public PersonViewModel()
{
   SaveDataCommand = new DelegatingCommand(SaveData, ()=> flag);
}

bool flag;

public bool IsValidObject
{
    get 
    { 
        var context = new ValidationContext(EmpInfo, null, null);
        flag = Validator.TryValidateObject(EmpInfo, context, null, true);
        SaveDataCommand.RaiseCanExecute() // don't know what type of command you have but you should have a method which you can call something with Canexecute
        return flag;
    }
}

public bool ValidateEmployeeObject()
{
  return IsValidObject;
}

public ICommand SaveDataCommand { get;  }

我对 INotifyDataErrorInfo 不是很熟悉,我不知道你的 IsValidObject 在什么时候被调用,但我认为像上面这样的东西应该可以工作。

于 2016-08-19T09:02:29.430 回答