验证 WPF 文本框中的十进制数字 [英] Validate decimal numbers in a WPF TextBox

查看:38
本文介绍了验证 WPF 文本框中的十进制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 WPF 文本框上实现十进制数验证.

I want to implement decimal number validation on WPF TextBoxes.

我找到了 WPF TextBox 验证数值的答案,但我想要一个也允许十进制值(浮点/双精度)的文本框.

I found an answer for WPF TextBox validation on numeric values, but I want a TextBox that allows decimal values (float/double) as well.

我该怎么做?

public static bool GetIsDecimal(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsDecimalProperty);
    }

    public static void SetIsDecimal(DependencyObject obj, bool value)
    {
        obj.SetValue(IsDecimalProperty, value);
    }

    public static readonly DependencyProperty IsDecimalProperty =
 DependencyProperty.RegisterAttached("IsDecimal", 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) || (newChar == '.'));
    }
}

我正在使用上面的代码.代码 (Char.IsNumber(newChar) || (newChar == '.')) 将检查数字和小数.所以现在 TextBox 只允许数字和十进制 (.).但问题是,我可以输入多个小数点(例如,1.01.22.011).所以我想限制输入多个小数点.

I am using the above code. The code (Char.IsNumber(newChar) || (newChar == '.')) will check for numbers and decimals. So now the TextBox allows only numbers and decimal(.). But the problem is that, I can enter multiple decimal points (For example, 1.01.22.011). So I want to restrict entering multiple decimal points.

推荐答案

你可以使用一个行为

    <TextBox MaxLength="17" MinWidth="205" HorizontalContentAlignment="Right"
             Text="{Binding Path=Amount, Mode=TwoWay, UpdateSourceTrigger=LostFocus, ValidatesOnExceptions=true,  ValidatesOnDataErrors=true, NotifyOnValidationError=True,
                             TargetNullValue={x:Static System:String.Empty},
                             Converter={StaticResource MyStringToDecimalConverter},ConverterParameter=#\,##0.00}">            
        <i:Interaction.Behaviors>                
            <Behaviors:TextBoxInputBehavior InputMode="DecimalInput" JustPositivDecimalInput="false"//>
        </i:Interaction.Behaviors>
    </TextBox>

behavior.cs:

behavior.cs:

public class TextBoxInputBehavior : Behavior<TextBox>
{
    const NumberStyles validNumberStyles = NumberStyles.AllowDecimalPoint |
                                               NumberStyles.AllowThousands |
                                               NumberStyles.AllowLeadingSign;
    public TextBoxInputBehavior()
    {
        this.InputMode = TextBoxInputMode.None;
        this.JustPositivDecimalInput = false;
    }

    public TextBoxInputMode InputMode { get; set; }


    public static readonly DependencyProperty JustPositivDecimalInputProperty =
     DependencyProperty.Register("JustPositivDecimalInput", typeof(bool),
     typeof(TextBoxInputBehavior), new FrameworkPropertyMetadata(false));

    public bool JustPositivDecimalInput
    {
        get { return (bool)GetValue(JustPositivDecimalInputProperty); }
        set { SetValue(JustPositivDecimalInputProperty, value); }
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.PreviewTextInput += AssociatedObjectPreviewTextInput;
        AssociatedObject.PreviewKeyDown += AssociatedObjectPreviewKeyDown;

        DataObject.AddPastingHandler(AssociatedObject, Pasting);

    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.PreviewTextInput -= AssociatedObjectPreviewTextInput;
        AssociatedObject.PreviewKeyDown -= AssociatedObjectPreviewKeyDown;

        DataObject.RemovePastingHandler(AssociatedObject, Pasting);
    }

    private void Pasting(object sender, DataObjectPastingEventArgs e)
    {
        if (e.DataObject.GetDataPresent(typeof(string)))
        {
            var pastedText = (string)e.DataObject.GetData(typeof(string));

            if (!this.IsValidInput(this.GetText(pastedText)))
            {
                System.Media.SystemSounds.Beep.Play();
                e.CancelCommand();
            }
        }
        else
        {
            System.Media.SystemSounds.Beep.Play();
            e.CancelCommand();
        }
    }

    private void AssociatedObjectPreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Space)
        {
            if (!this.IsValidInput(this.GetText(" ")))
            {
                System.Media.SystemSounds.Beep.Play();
                e.Handled = true;
            }
        }
    }

    private void AssociatedObjectPreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        if (!this.IsValidInput(this.GetText(e.Text)))
        {
            System.Media.SystemSounds.Beep.Play();
            e.Handled = true;
        }
    }

    private string GetText(string input)
    {
        var txt = this.AssociatedObject;
        var realtext = txt.Text.Remove(txt.SelectionStart, txt.SelectionLength);
        var newtext = realtext.Insert(txt.CaretIndex, input);

        return newtext;
    }

    private bool IsValidInput(string input)
    {
        switch (InputMode)
        {
            case TextBoxInputMode.None:
                return true;
            case TextBoxInputMode.DigitInput:
                return CheckIsDigit(input);

            case TextBoxInputMode.DecimalInput:
                decimal d;
                //wen mehr als ein Komma
                if (input.ToCharArray().Where(x => x == ',').Count() > 1)
                    return false;

                if (input.Contains("-"))
                {
                    //minus einmal am anfang zulässig
                    if (!this.JustPositivDecimalInput && input.IndexOf("-") == 0  && input.Length == 1)
                        return true;
                    else
                    {
                        var result = decimal.TryParse(input, validNumberStyles, CultureInfo.CurrentCulture, out d);
                        return result;
                    }
                }
                else
                {
                    var result = decimal.TryParse(input, validNumberStyles, CultureInfo.CurrentCulture, out d);
                    return result;
                }


            default: throw new ArgumentException("Unknown TextBoxInputMode");

        }
        return true;
    }

    private bool CheckIsDigit(string wert)
    {
        return wert.ToCharArray().All(Char.IsDigit);
    }
}

public enum TextBoxInputMode
{
    None,
    DecimalInput,
    DigitInput
}

这篇关于验证 WPF 文本框中的十进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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