开发者

how we put integer validation on text box in wpf

I'm making a Wpf Application. I want to put validations on integer an开发者_如何学编程d character textboxes.

How can I achieve it?


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");
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜