Windows 8 Metro: Implementing Validation

孤街浪徒 提交于 2019-12-01 18:09:46

问题


I just googled for about 2 hours and didn't find anything.

Given the following scenario:

public class Person
{
    [Required]
    public string Name { get; set; }
}

XAML:

<TextBox Text="{Binding Name, Mode=TwoWay}" />

How would you go about automatically get feedback of the validation on the UI like there is in MVC3?

(Oh, and I really don't care at the moment if I would be able to use the integrated DataAnnotations like [Required] or not)

Any help is much appreciated!


回答1:


I added something to the WinRT XAML Toolkit. It's called TextBoxValidationExtensions and allows you to define the validation like this:

<TextBox
    Width="400"
    HorizontalAlignment="Left"
    xyzc:TextBoxValidationExtensions.Format="NonEmptyNumeric"
    xyzc:TextBoxValidationExtensions.InvalidBrush="Red"
    xyzc:TextBoxValidationExtensions.ValidBrush="Green" />

The formats are currently defined as:

[Flags]
public enum ValidTextBoxFormats
{
    Any = 0,
    NonEmpty = 1,
    Numeric = 2,
    NonEmptyNumeric = 3
}

The entire code is a bit too long to share here. You can take out the code from CodePlex and modify to your liking - add new validation rules, modify default brushes etc.




回答2:


Just use events in the particular XAML control, like if in textbox use pointerexited and write a function in the code behind to perform the required validation.




回答3:


It seems, based on the support provided in the framework, that your extraordinary idea to "Validate" a text field is so rare and uncommon that it needs not to be considered at all. We all know that if what you wanted to do was at all common or useful, there would be some consideration for such a feature in such a well designed and comprehensive framework.



来源:https://stackoverflow.com/questions/10620145/windows-8-metro-implementing-validation

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