Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows 8 Metro: Implementing Validation

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!

like image 884
Shion Avatar asked May 16 '12 13:05

Shion


1 Answers

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.

like image 197
Filip Skakun Avatar answered Oct 05 '22 08:10

Filip Skakun