Rob*_* J. 4 c# validation wpf win-universal-app
我试图弄清楚如何在UWP下进行数据验证,但根据我发现的情况,基本上没有什么我可以实现的.
因此,我试图实现我的自定义验证逻辑.我现在遇到的问题是,我在一个TextBlock而不是直接在TextBox包含数据错误的特定信息下显示错误信息.
这就是我现在所做的:
public class Customer : ViewModel
{
private string _Name = default(string);
public string Name { get { return _Name; } set { SetProperty(ref _Name, value); OnPropertyChanged("IsValid"); } }
private string _Surname = default(string);
public string Surname { get { return _Surname; } set { SetProperty(ref _Surname, value); OnPropertyChanged("IsValid"); } }
private DateTime _DateOfBirth = default(DateTime);
public DateTime DateOfBirth { get { return _DateOfBirth; } set { SetProperty(ref _DateOfBirth, value); OnPropertyChanged("IsValid"); } }
public int ID { get; set; }
public bool IsValid
{
get
{
//restart error info
_ErrorInfo = default(string);
if (string.IsNullOrWhiteSpace(Name))
_ErrorInfo += "Name cannot be empty!" + Environment.NewLine;
if (string.IsNullOrWhiteSpace(Surname))
_ErrorInfo += "Surname cannot be empty!" + Environment.NewLine;
//raise property changed
OnPropertyChanged("ErrorInfo");
return !string.IsNullOrWhiteSpace(Name) &&
!string.IsNullOrWhiteSpace(Surname);
}
}
private string _ErrorInfo = default(string);
public string ErrorInfo { get { return _ErrorInfo; } set { SetProperty(ref _ErrorInfo, value); } }
}
Run Code Online (Sandbox Code Playgroud)
题:
如何调整我的代码,以便不是让一个标签包含所有错误信息,我可以在每个文本框下分配标签并在那里显示验证错误?我应该使用字典吗?如果是,我如何将其绑定到我的视图?