WPF-validationrule data verification

Source: Internet
Author: User

XAML

< Window X: Class = "Wpfapplication1.window1"
Xmlns
= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x
= "Http://schemas.microsoft.com/winfx/2006/xaml"
Title
= "Window1" Height = "192" Width = "537"
Xmlns: Local
= "CLR-namespace: wpfapplication1" >

< Window. Resources >
<! -- Judgment Rules -->
< Local: numberrangerule X: Key = "Numberrangerule" > </ Local: numberrangerule >
</ Window. Resources >

< Grid Name = "Grid" Cliptobounds = "False" Height = "129" Width = "297" >
< Label Horizontalalignment = "Left" Margin = "25, 53, 0, 0" Name = "Label2" Width = "49" Height = "28" Verticalalignment = "TOP" > Age: </ Label >
<! -- BIND error content to tooltip -->
< Textbox Margin =" Tooltip =" {Binding relativesource = {relativesource self}, Path = (validation. Errors) [0]. errorcontent} "  
Name
= "Txtage" Horizontalalignment = "Left" Width = "101" >
< Textbox. Text >
<! -- Bind age data and judgment rules and set the maximum and minimum values -->
< Binding Path = "Age" Updatesourcetrigger = "Propertychanged" >
< Binding. validationrules >
< Local: numberrangerule Min = "0" Max = "128"   />
</ Binding. validationrules >
</ Binding >
</ Textbox. Text >
</ Textbox >
</ Grid >
</ Window >

 

 

C #

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. windows;
Using System. Windows. controls;
Using System. Windows. Data;
Using System. Windows. documents;
Using System. Windows. input;
Using System. Windows. Media;
Using System. Windows. Media. imaging;
Using System. Windows. Navigation;
Using System. Windows. shapes;

UsingSystem. componentmodel;

Namespace Wpfapplication1
{
///   <Summary>
/// Interaction logic of window1.xaml
///   </Summary>
Public   Partial   Class Window1: Window
{
Public Person;
Public Window1 ()
{
Initializecomponent ();

// Create and bind sample data
Person =   New Person ();
Person. Name
=   " Kill " ;
Person. Age
=   21 ;
This . Txtage. datacontext = Person;
}
}

// the data class must inherit inotifypropertychanged
Public class person: inotifypropertychanged
{< br>
Public event propertychangedeventhandler propertychanged;

string name;
Public string name
{< br>
Get { return name ;}
set {name = value ;}
}

int age;
Public int age
{< br>
Get { return age ;}
set {age = value ;}
}

PublicPerson (){}
}

// Write the Judgment Rule class inheriting validationrule
// Rewrite the validate method to complete your judgment.
Public   Class Numberrangerule: validationrule
{
Int Min;
Public   Int Min
{
Get { Return Min ;}
Set {Min = Value ;}
}

int MAX;
Public int max
{< br>
Get { return MAX ;}
set {max = value ;}
}

Public   Override Validationresult validate ( Object Value, system. Globalization. cultureinfo)
{
Int Number;
If ( ! Int . Tryparse (( String ) Value, Out Number ))
{
Return   New Validationresult ( False , " Invalid Number Format " );
}

If (Number < Min | Number > Max)
{
Return   New Validationresult ( False , String . Format ( " Number out of range ({0}-{1 }) " , Min, max ));
}

ReturnValidationresult. validresult;
}
}

}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.