Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to implement decimal number validation on WPF TextBoxes.

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

How can I do this?

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 == '.'));
    }
}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
284 views
Welcome To Ask or Share your Answers For Others

1 Answer

you can use a behavior

    <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:

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...