Windows 10 (Universal Windows App) data validation

╄→尐↘猪︶ㄣ 提交于 2020-02-02 04:23:06

问题


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.

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.

This is what I do at the moment:

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); } }

}

Question:

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?


回答1:


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.




回答2:


You can make a flyout inside a textbox.

As soon as the textbox loses focus with wrong input, the flyout shows up .

You can set the placament of the flyout on top/bottom/side of the textbox.

Best of luck !




回答3:


The problem with Prism is that it uses a string indexer. But Bind in uwp just will not allow string indexes... Integers only! There are also some key features lacking such as coordination between entity view models and between them and the context.

I've done some R&D and it seems that the following are key elements of a good validator in uwp - use of strings as the binding target, to avoid dropping conversion exceptions - tracking conversion errors separately from validation errors - base class for the validating view model AND automatically generated derived classes specifying the property names - events to tie multiple view models together so that multiple parts of the ui are kept consistent - centralized error count and save / revert ability associated with the context

Anything out there that can do that? If so then I haven't found it yet.

sjb



来源:https://stackoverflow.com/questions/32892901/windows-10-universal-windows-app-data-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!