在我以前的问题中,我得到了一个解决方案,如何手动检查验证结果https://stackoverflow.com/questions/39031783/screen-validation-with-data-annotations-on-button-click-in-using-inotifydataerro/39033124#39033124
我有一个模型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
最初可以正常工作。如果我更新FirstName
和LastName
中的值,则属性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;
}
}
我在这里介绍了一种方法ValidateEmployeeObject()
,它返回TRUE
/ FALSE
,类似于属性IsValidObject
。我在@adminSoftDK在评论部分中提出的 CanExecute
SaveDataCommand
块中绑定了此方法。这种方法也失败了......
如何根据上述验证更新“保存”按钮以启用“启用”。
答案 0 :(得分: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被调用到什么时候,但我认为上面的内容应该有效。