WPF c#中基于INotifyDataErrorInfo的Bool属性更新
本文关键字:Bool 属性 更新 INotifyDataErrorInfo WPF | 更新日期: 2023-09-27 18:03:00
在我之前的问题中,我得到了一个如何手动检查验证结果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;
}
}
模型类:Person
public class Person
{
[Required]
public string FirstName { get; set; }
public string MiddleName { get; set; }
[Required]
public string LastName { get; set; }
}
My XAML is
<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
块中绑定了这个Method。这种方法也失败了…
我如何更新保存按钮使基于上述验证的启用
如果你尝试这样做
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被调用,但我认为像上面的东西应该工作。