数值上的 WPF 文本框验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13251506/
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-09-13 06:06:02  来源:igfitidea点击:

WPF Text Box Validation on numeric value

wpfwpf-controls

提问by Rajeev Kumar

if have a text box in xaml. i want only numeric values can be write in textbox to validate on button. how can i do it ?

如果在 xaml 中有一个文本框。我只想在文本框中写入数值以在按钮上进行验证。我该怎么做 ?

<TextBox x:Name="txtLevel" Grid.Column="1" HorizontalAlignment="Stretch"></TextBox>

回答by Vida Fs

You can use numericUppDowninstead of textboxso as to have only numeric input. That is the way to do it.

您可以使用numericUppDown代替textbox以便只有数字输入。这就是这样做的方法。

回答by Andy

I would create a class to host all of the additions to textbox that you want to make like below, the bit that stops you putting numbers is in the event handler for PreviewTextInput, if it's not numeric I just say the event is handled and the textbox never gets the value.

我将创建一个类来托管您想要添加到文本框的所有添加内容,如下所示,阻止您输入数字的位位于 PreviewTextInput 的事件处理程序中,如果它不是数字,我只是说事件已处理并且文本框永远得不到价值。

public class TextBoxHelpers : DependencyObject
{

    public static bool GetIsNumeric(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsNumericProperty);
    }

    public static void SetIsNumeric(DependencyObject obj, bool value)
    {
        obj.SetValue(IsNumericProperty, value);
    }

    // Using a DependencyProperty as the backing store for IsNumeric.  This enables animation, styling, binding, etc...
           public static readonly DependencyProperty IsNumericProperty =
        DependencyProperty.RegisterAttached("IsNumeric", typeof(bool), typeof(TextBoxHelpers), new PropertyMetadata(false, new PropertyChangedCallback((s, e) =>
            {
                TextBox targetTextbox = s as TextBox;
                if (targetTextbox != null)
                {
                    if ((bool)e.OldValue && !((bool)e.NewValue))
                    {
                        targetTextbox.PreviewTextInput -= targetTextbox_PreviewTextInput;

                    }
                    if ((bool)e.NewValue)
                    {
                        targetTextbox.PreviewTextInput += targetTextbox_PreviewTextInput;
                        targetTextbox.PreviewKeyDown += targetTextbox_PreviewKeyDown;
                    }
                }
            })));

    static void targetTextbox_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        e.Handled = e.Key == Key.Space;
    }

    static void targetTextbox_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        Char newChar = e.Text.ToString()[0];
        e.Handled = !Char.IsNumber(newChar);
    }
}

To use the helper class with attached property in XAML you need to point a namespace to it then use it like this.

要在 XAML 中使用带有附加属性的辅助类,您需要将命名空间指向它,然后像这样使用它。

<TextBox local:TextBoxHelpers.IsNumeric="True" />

回答by photoscar

Use wpf behavior control. Example http://wpfbehaviorlibrary.codeplex.com/

使用 wpf 行为控制。示例 http://wpfbehaviorlibrary.codeplex.com/