0

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 ?

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

3 Answers 3

4

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.

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.

<TextBox local:TextBoxHelpers.IsNumeric="True" />
Sign up to request clarification or add additional context in comments.

6 Comments

what if the value enter is 3 5, i.e white space between numeric values. i want only numeric value > 0 with no white space. It actually works fine for non numeric characters but not for white space
Just add the check for the space too e.Handled = !Char.IsNumber(e.Text.ToString()[0]) && !string.IsNullOrWhiteSpace (e.Text.ToString()[0]);
string.IsNullOrWhiteSpace is not a found within string class !!
oops, don't know what version of .net that came in, you can just do the check for null then do a check for " "
Well I didn't know that! I've updated again, you can use key down to block the spaces.
|
4

You can use numericUppDown instead of textbox so as to have only numeric input. That is the way to do it.

1 Comment

I am using .net 3.5 Framework, numericUpDown control is not available !!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.