More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Thursday, February 9, 2017 9:36:05 PM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 11/23/2008(UTC)
Posts: 522

WPF custom validation

See the custom validation rule <local:ByteValidation></local:ByteValidation> in class ByteValidation which inherits from ValidationRule below. Note that the ExceptionValidationRule can be used together with the custom validation rule side-by-side.

XAML
Code:
            <TextBox x:Name="TextBox1" Grid.Row="0" Grid.Column="1" MinWidth="100">
                <TextBox.Text>
                    <Binding Path="Byte">
                        <Binding.ValidationRules>
                            <ExceptionValidationRule></ExceptionValidationRule>
                            <local:ByteValidation></local:ByteValidation>
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>


Code:
using System.Globalization;
using System.Windows.Controls;

    public class ByteValidation : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            byte val= 0;
            if (value != null && byte.TryParse(value.ToString(), out val))
            {
                //more custom validation if required

                return new ValidationResult(true, null);
            }
            return new ValidationResult(false, "Value must be an integer between 0 and 255");
        }
    }
}

Edited by user Tuesday, February 14, 2017 8:50:52 PM(UTC)  | Reason: Not specified

Sponsor
Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.