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 8351433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:37:37+00:00 2026-06-09T08:37:37+00:00

I have a textbox in xaml here is code in .cs file: public static

  • 0

I have a textbox in xaml here is code in .cs file:

public static readonly DependencyProperty dp =
            DependencyProperty.Register(
                "result", typeof( uint ), typeof( ui ),
                new FrameworkPropertyMetadata( ( uint )100, new PropertyChangedCallback( ResultChanged ) ) );


private static void ResultChanged(
            DependencyObject d, DependencyPropertyChangedEventArgs e )
        {
            var input = ( ui )d;
            var value = ( uint )e.NewValue;
        }

Above code works great it is not allowing any alphabets or invalid characters to be entered in the textbox. But how can i change above code so that user will not be able to enter “0” in the textbox? So basically allow all uint except 0.

  • 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-09T08:37:39+00:00Added an answer on June 9, 2026 at 8:37 am

    There are many ways to do this….the simplest and easiest is just to use the NumericUpDown control from the Extended WPF Toolkit….set the min/max range and you are sorted.

    http://wpftoolkit.codeplex.com/wikipage?title=NumericUpDown


    If you do want to do the logic all yourself then….

    You could specify a “Coercion Callback” and/or a “Validate Callback” in the Register of the Dependency Property depending on your needs/requirements i.e. do you need to fix the value to a range, or show an error indicator to the user?

    “Coercion” is where you adjust the value to something else e.g. in this case you might decide to adjust any value of 0, to 1 (or possibly null).

    “Validation” is where the value is checked and you say if it is correct or not (by returning true of false)…if you return false…then an exception gets raised.

    You use ValidatesOnException on a Binding to the DependencyProperty in order for that “error” to be propagated to the user interface in the way of display of an ErrorTemplate (the default one shows a red border around a control).

    • http://www.shujaat.net/2010/07/wpf-validation-validatevaluecallback.html

    • http://msdn.microsoft.com/en-us/library/ms745795.aspx

    • http://wpf.2000things.com/2010/11/11/122-validating-a-dependency-property/

    • http://wpf.2000things.com/2010/11/12/123-coercing-a-dependency-property/

    • http://wpf.2000things.com/2010/12/09/150-an-example-of-using-propertychanged-and-coercevalue-callbacks/

    In this case I will show just using the Coercion Callback…as I’m presuming you don’t want an invalid value to be waiting inside your TextBox. If you want the Validate one…then see the links above. (you don’t need a Changed callback, so that’s set to null).

    public static readonly DependencyProperty dp =
        DependencyProperty.Register(
            "result", typeof( uint ), typeof( ui ),
            new FrameworkPropertyMetadata(
                ( uint )100, 
                null,
                new CoerceValueCallback( ResultCoerceValue )
            )
        );
    
    
    private static object ResultCoerceValue 
        (DependencyObject depObj, object baseValue)
    {
        uint coercedValue = (uint)baseValue;
    
        if ((uint)baseValue == 0)
            coercedValue = 1; // might be able to set to null...but not sure on that.
    
        return coercedValue;
    }
    

    There are also different techniques you can use like ValidationRules, possibly MaskedTextBox, and using PreviewTextInput.

    • http://social.msdn.microsoft.com/Forums/en/wpf/thread/990c635a-c14e-4614-b7e6-65471b0e0e26

    • How do I get a TextBox to only accept numeric input in WPF?

    • http://weblogs.asp.net/monikadyrda/archive/2009/06/24/wpf-textbox-validation.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a xaml code: <Grid> <WrapPanel> <TextBox ></TextBox> <Button Content=GetIt /> </WrapPanel> </Grid>
i have the following datatemplate in xaml and here i have textbox in datatemplate
I have some XAML <ItemsControl Name=mItemsControl> <ItemsControl.ItemTemplate> <DataTemplate> <TextBox Text={Binding Mode=OneWay} KeyUp=TextBox_KeyUp/> </DataTemplate> </ItemsControl.ItemTemplate>
Consider a XAML TextBox in Win Phone 7. <TextBox x:Name=UserNumber /> The goal here
How do I recreate the following XAML databinding in code? I have most of
How can I do this in XAML: PSEUDO-CODE: <TextBox Text={Binding Password} Type=Password/> so that
I have the following XAML code as part of a custom control: <telerik:RadTreeView x:Name=treeModules>
I have added a dialog to my WPF application. Here's the Xaml: <cs:CarSystemDialog x:Class=CarSystem.CustomControls.EditHotListDialog
I have Textbox with blue Background and white Foreground . When entering a text
I have: Textbox(Multi-line) Panel Different controls inside panel (buttons,textbox) Scenario: I need to add

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.