C# 如何只接受 WPF 文本框中的整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14813960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:05:12  来源:igfitidea点击:

How to accept only integers in a WPF textbox

c#wpfxaml

提问by Dunkey

Do you know how to restrict user input in textbox, this textbox only accepts integer? By the way I'm developing for Windows 8. I've tried what I searched from SO and from Google but it's not working,

你知道如何限制用户在文本框中输入,这个文本框只接受整数吗?顺便说一下,我正在为 Windows 8 开发。我已经尝试了从 SO 和 Google 搜索的内容,但它不起作用,

回答by GrandMasterFlush

You could use a integer up down control. There's one in the WPF toolkit that will do the trick:

您可以使用整数上下控制。WPF 工具包中有一个可以解决这个问题:

https://wpftoolkit.codeplex.com/wikipage?title=IntegerUpDown

https://wpftoolkit.codeplex.com/wikipage?title=IntegerUpDown

回答by dutzu

At the most raw level you can intercept the KeyUpevent or TextChangedto see what char is being added and remove it if it cannot be parsed to Int.

在最原始的级别,您可以拦截KeyUp事件或TextChanged查看正在添加的字符,如果无法解析为 Int,则将其删除。

Also check - Only accept digits for textboxand Masking Textbox to accept only decimals

还要检查 -仅接受文本框掩码文本框的数字以仅接受小数

回答by KyleMit

If you don't want to download the WPF ToolKit (which has both the IntegerUpDown control or a MaskedTextBox), you can implement it yourself as adapted from this article on Masked TextBox In WPFusing the UIElement.PreviewTextInputand DataObject.Pastingevents.

如果您不想下载 WPF ToolKit(它同时具有 IntegerUpDown 控件或 MaskedTextBox),您可以使用和事件根据这篇关于WPF中的Masked TextBox 的文章自行实现。UIElement.PreviewTextInputDataObject.Pasting

Here's what you would put in your window:

以下是您要放在窗口中的内容:

<Window x:Class="WpfApp1.MainWindow" Title="MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel Orientation="Vertical" Width="100" Height="100"  HorizontalAlignment="Left" VerticalAlignment="Top">

        <TextBlock Name="NumericLabel1"  Text="Enter Value:"  />
        <TextBox   Name="NumericInput1" 
                   PreviewTextInput="MaskNumericInput" 
                   DataObject.Pasting="MaskNumericPaste"  />
    </StackPanel>
</Window>

And then implement the C# in your codebehind:

然后在你的代码隐藏中实现 C#:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void MaskNumericInput(object sender, TextCompositionEventArgs e)
    {
        e.Handled = !TextIsNumeric(e.Text);
    }

    private void MaskNumericPaste(object sender, DataObjectPastingEventArgs e)
    {
        if (e.DataObject.GetDataPresent(typeof(string)))
        {
            string input = (string)e.DataObject.GetData(typeof(string));
            if (!TextIsNumeric(input)) e.CancelCommand();
        }
        else
        {
            e.CancelCommand();
        }
    }

    private bool TextIsNumeric(string input)
    {
        return input.All(c => Char.IsDigit(c) || Char.IsControl(c));
    }
}

回答by Nathan Hillyer

public class IntegerTextBox : TextBox
{
    protected override void OnTextChanged(TextChangedEventArgs e)
    {
        base.OnTextChanged(e);

        Text = new String(Text.Where(c => Char.IsDigit(c)).ToArray());
        this.SelectionStart = Text.Length;
    }
}