Click here to Skip to main content
12,749,593 members (30,806 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 31 Mar 2011

Getting control of your numbers

, 1 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple mechanism to limit the input of data to a TextBox so that it only accepted the relevant numeric amount.

Today on Code Project, one of the regulars asked how to set up a textbox so that it only accepted a currency amount. He was concerned that there doesn’t seem to be a simple mechanism to limit the input of data so that it only accepted the relevant numeric amount. Well, this is a feature I recently added into Goldlight, so I thought I’d post it here, along with an explanation of how it works.

Basically, and this will come as no surprise to you, it’s an Attached Behavior that you associate to the TextBox. There are many numeric only behaviors out there, so this one goes a little bit further. First of all, if you want, you can limit it to integers by setting AllowDecimal to false. If you want to limit it to a set number of decimal places, set DecimalLimit to the number of decimal places. If you don’t want to allow the developer to use negative numbers, set AllowNegatives to false. It’s that simple, so the solution to the problem would be to add the behaviour to the TextBox like this:

<TextBox Text="{Binding Price}">
    <gl:NumericTextBoxBehavior AllowNegatives="False" />

The full code to do this is shown below:

namespace Goldlight.Extensions.Behaviors
  using System.Windows.Controls;
  using System.Windows.Interactivity;
  using System.Windows.Input;
  using System.Text.RegularExpressions;
  using System.Windows;
  using System.Globalization;

  /// <span class="code-SummaryComment"><summary>

The clever thing is that this behavior doesn’t allow the user to paste an incorrect value in either – the paste operation is subject to the same rules as directly entering the value in the first place.

Anyway, I hope this behavior is as much use to you as it is to me.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pete O'Hanlon
United Kingdom United Kingdom
A developer for over 30 years, I've been lucky enough to write articles and applications for Code Project as well as the Intel Ultimate Coder - Going Perceptual challenge. I live in the North East of England with 2 wonderful daughters and a wonderful wife.

I am not the Stig, but I do wish I had Lotus Tuned Suspension.

You may also be interested in...


Comments and Discussions

BugDoes not work with UpdateSourceTrigger=PropertyChanged Pin
d_weyermann24-Jan-12 22:51
memberd_weyermann24-Jan-12 22:51 
GeneralMy vote of 5 Pin
gardnerp3-May-11 4:40
membergardnerp3-May-11 4:40 
GeneralRe: My vote of 5 Pin
Pete O'Hanlon10-May-11 11:16
mvpPete O'Hanlon10-May-11 11:16 
QuestionPRE tags language attribute missing? Pin
Sandeep Mewara1-Apr-11 1:49
mvpSandeep Mewara1-Apr-11 1:49 
AnswerRe: PRE tags language attribute missing? Pin
Pete O'Hanlon1-Apr-11 2:30
mvpPete O'Hanlon1-Apr-11 2:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170215.1 | Last Updated 1 Apr 2011
Article Copyright 2011 by Pete O'Hanlon
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid