如何限制用户在 TextBox 中输入的值? [英] How can I restrict the values a user enters into TextBox?

查看:30
本文介绍了如何限制用户在 TextBox 中输入的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的用户可以在文本框中输入值.在某些情况下,需要将值限制在某个范围内,例如0 到 1,或 1 到 50.在其他情况下,他们最多只能输入 13 或 20 个字符.

Users of my application can enter values into TextBoxes. In some cases the values need to be restricted to a certain range, e.g. 0 to 1, or 1 to 50. In other cases they can only enter upto 13 or 20 characters depending.

我可以使用 Binding.StringFormat 来强制执行这些范围吗?如果是这样,

Can I use Binding.StringFormat to enforce these ranges? If so,

  • 我需要指定什么来将值限制为特定字符长度?
  • 我需要指定什么来将值限制在某个范围内?

如果没有,实现此目的的最佳方法是什么?

If not, what's the best way to implement this?

推荐答案

您需要的是输入验证.对于初学者MSDN 上的本教程 将帮助您,有关 IDataErrorInfo 的更多信息(首选方式)可以在这里获得.

What you want is input validation. For starters this tutorial on MSDN will help you, more information on IDataErrorInfo (preferred way) can be obtained here.

这个问题已经是一个关于 SO 的主题:WPF 数据绑定和验证规则最佳做法

This question was already a topic on SO: WPF Data Binding and Validation Rules Best Practices

这篇关于如何限制用户在 TextBox 中输入的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆