Windows 10(通用 Windows 应用)数据验证 [英] Windows 10 (Universal Windows App) data validation
问题描述
我正在研究如何在UWP下进行数据验证,但根据我所发现的,基本上我还没有什么可以实现的.
I was trying to figure out how to do the data validation under UWP, but according to what I have found out, there is basically nothing I can implemented yet.
因此,我尝试实现我的自定义验证逻辑.我现在遇到的问题是,我在一个 TextBlock
上显示错误信息,而不是直接在包含数据错误的特定 TextBox
下显示.
Due to that I tried to implement my custom validation logic. Problem I have now is, that I am showing error information on one TextBlock
rather than directly under the specific TextBox
which contains data error.
这就是我目前所做的:
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); } }
}
问题:
如何调整我的代码,而不是让一个包含所有错误信息的标签,我可以在每个文本框下分配标签并在那里显示验证错误?我应该为此使用字典吗?如果是,我如何将它绑定到我的视图?
How to adjust my code, so that rather than having one label with all error information, I can assign label under each textbox and display validation error there? Should I use Dictionary for this? If yes, how can I bind it to my View?
推荐答案
我很快就成为了使用 Prism 的粉丝,请看这个精彩的演示 用户输入验证与 UWP 上的棱镜和数据注释.
I have quickly become a fan of using Prism, see this wonderful demonstration User input validation with Prism and data annotations on the UWP.
它比我在这里输入的任何东西都要好.
Its better than anything I could type here.
这篇关于Windows 10(通用 Windows 应用)数据验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!