<small id='yEPPl'></small><noframes id='yEPPl'>

  • <legend id='yEPPl'><style id='yEPPl'><dir id='yEPPl'><q id='yEPPl'></q></dir></style></legend>
    <i id='yEPPl'><tr id='yEPPl'><dt id='yEPPl'><q id='yEPPl'><span id='yEPPl'><b id='yEPPl'><form id='yEPPl'><ins id='yEPPl'></ins><ul id='yEPPl'></ul><sub id='yEPPl'></sub></form><legend id='yEPPl'></legend><bdo id='yEPPl'><pre id='yEPPl'><center id='yEPPl'></center></pre></bdo></b><th id='yEPPl'></th></span></q></dt></tr></i><div id='yEPPl'><tfoot id='yEPPl'></tfoot><dl id='yEPPl'><fieldset id='yEPPl'></fieldset></dl></div>

        <bdo id='yEPPl'></bdo><ul id='yEPPl'></ul>

        <tfoot id='yEPPl'></tfoot>

        创建只接受数字的 WPF 文本框

        Create WPF TextBox that accepts only numbers(创建只接受数字的 WPF 文本框)
            <tfoot id='02pyy'></tfoot>
          • <i id='02pyy'><tr id='02pyy'><dt id='02pyy'><q id='02pyy'><span id='02pyy'><b id='02pyy'><form id='02pyy'><ins id='02pyy'></ins><ul id='02pyy'></ul><sub id='02pyy'></sub></form><legend id='02pyy'></legend><bdo id='02pyy'><pre id='02pyy'><center id='02pyy'></center></pre></bdo></b><th id='02pyy'></th></span></q></dt></tr></i><div id='02pyy'><tfoot id='02pyy'></tfoot><dl id='02pyy'><fieldset id='02pyy'></fieldset></dl></div>

                • <bdo id='02pyy'></bdo><ul id='02pyy'></ul>
                  <legend id='02pyy'><style id='02pyy'><dir id='02pyy'><q id='02pyy'></q></dir></style></legend>
                    <tbody id='02pyy'></tbody>

                • <small id='02pyy'></small><noframes id='02pyy'>

                  本文介绍了创建只接受数字的 WPF 文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想创建一个只接受特定范围内数值的文本框.实现此类 TextBox 的最佳方法是什么?

                  I would like to create a TextBox that only accepts numeric values, in a specific range. What is the best way to implement such TextBox?

                  我想过派生 TextBox 并覆盖 TextProperty 的验证和强制.但是,我不确定如何执行此操作,并且我知道通常不建议派生 WPF 控件.<小时>
                  我需要的是一个非常基本的文本框,它可以过滤掉所有不是数字的按键.实现它的最简单方法是处理 TextBox.PreviewTextInput 事件:

                  I thought about deriving TextBox and to override the validation and coercion of the TextProperty. However, I am not sure how to do this, and I understand that deriving WPF control is generally not recommended.



                  What I needed was a very basic textbox that filters out all key presses which are not digits. The easiest way to achieve it is to handle the TextBox.PreviewTextInput event:

                  private void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
                  {
                      int result;
                      if (!validateStringAsNumber(e.Text,out result,false))
                      {
                          e.Handled = true;
                      }
                  }
                  

                  (validateStringAsNumber 是我主要使用 Int.TryParse 的函数)

                  (validateStringAsNumber is my function that primarily use Int.TryParse)

                  一些建议的解决方案可能更好,但对于我需要的简单功能,这个解决方案是最容易和最快实施的,同时足以满足我的需求.

                  Some of the suggested solutions are probably better, but for the simple functionality I needed this solution is the easiest and quickest to implement while sufficient for my needs.

                  推荐答案

                  到目前为止,我看到的大多数实现都是使用 PreviewTextInput 事件来实现正确的掩码行为.这个继承自TextBox和这个使用附加属性.两者都使用 .Net 的 MaskedTextProvider 来提供正确的掩码行为,但如果您只想要一个简单的仅限数字"文本框,则不需要此类.

                  Most implementations I have seen so far are using the PreviewTextInput event to implement the correct mask behavior. This one inherits from TextBox and this one uses attached properties. Both use .Net's MaskedTextProvider to provide the correct mask behaviour, but if you just want a simple 'numbers only' textbox you don't need this class.

                  这篇关于创建只接受数字的 WPF 文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to keep the Text of a Read only TextBox after PostBack()?(PostBack()之后如何保留只读文本框的文本?)
                  Winforms Textbox - Using Ctrl-Backspace to Delete Whole Word(Winforms 文本框 - 使用 Ctrl-Backspace 删除整个单词)
                  C# - Add button click events using code(C# - 使用代码添加按钮单击事件)
                  Multi-color TextBox C#(多色文本框 C#)
                  How can i set the caret position to a specific index in passwordbox in WPF(如何将插入符号位置设置为 WPF 密码框中的特定索引)
                  C# Numeric Only TextBox Control(C# 纯数字文本框控件)
                  <tfoot id='lMVp5'></tfoot>

                      <i id='lMVp5'><tr id='lMVp5'><dt id='lMVp5'><q id='lMVp5'><span id='lMVp5'><b id='lMVp5'><form id='lMVp5'><ins id='lMVp5'></ins><ul id='lMVp5'></ul><sub id='lMVp5'></sub></form><legend id='lMVp5'></legend><bdo id='lMVp5'><pre id='lMVp5'><center id='lMVp5'></center></pre></bdo></b><th id='lMVp5'></th></span></q></dt></tr></i><div id='lMVp5'><tfoot id='lMVp5'></tfoot><dl id='lMVp5'><fieldset id='lMVp5'></fieldset></dl></div>

                        <small id='lMVp5'></small><noframes id='lMVp5'>

                          <tbody id='lMVp5'></tbody>
                        <legend id='lMVp5'><style id='lMVp5'><dir id='lMVp5'><q id='lMVp5'></q></dir></style></legend>
                          <bdo id='lMVp5'></bdo><ul id='lMVp5'></ul>