1

I'm making a Wpf Application. I want to put validations on integer and character textboxes.
How can I achieve it?

1
  • Just a hint. For Integer validation and conversion use int.TryParse. For the rest - I donno, regular expressions in the worst case scenario. I just don't get what the big question is. Commented Sep 27, 2010 at 9:15

1 Answer 1

2

You can throw an Exception when values are out of range and use ValidationRules like this:

<TextBox>
    <TextBox.Text>
        <Binding Path="Number">
            <Binding.ValidationRules>
                <ExceptionValidationRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

More information can be found here: http://www.codeproject.com/KB/WPF/wpfvalidation.aspx

Update: In code behind you can do something like:

private int _Number;
public string Number
{
    get { return  _Number.ToString(); }
    set
    {
        if (!Int32.TryParse(value, out _Number))
        {
            throw new ApplicationException("Invalid integer number");
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I edited the answer to include code behind. hopefully it is what you expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.