Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8795361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:22:35+00:00 2026-06-13T23:22:35+00:00

if have a text box in xaml. i want only numeric values can be

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:22:36+00:00Added an answer on June 13, 2026 at 11:22 pm

    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" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some XAML <ItemsControl Name=mItemsControl> <ItemsControl.ItemTemplate> <DataTemplate> <TextBox Text={Binding Mode=OneWay} KeyUp=TextBox_KeyUp/> </DataTemplate> </ItemsControl.ItemTemplate>
What can I do if I want to have a text-box representing in real
I have a xaml code: <Grid> <WrapPanel> <TextBox ></TextBox> <Button Content=GetIt /> </WrapPanel> </Grid>
I have text box in which your adds values in comma separated values. Once
I have a text-box into which a user can input a comment. The comment
I have a text box and a button, if I enter some text in
Simple WPF question I have a text Box and an OK Button and I
I have a user control that has a text box and a button. I
I have a textbox and a button in my .xaml. When i click the
I have a text box for entering hex values for specifying color. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.