Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p><a href="http://wpftoolkit.codeplex.com/wikipage?title=NumericUpDown" rel="nofollow noreferrer">http://wpftoolkit.codeplex.com/wikipage?title=NumericUpDown</a></p> <hr> <p>If you do want to do the logic all yourself then....</p> <p>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?</p> <p>"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).</p> <p>"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.</p> <p>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).</p> <ul> <li><p><a href="http://www.shujaat.net/2010/07/wpf-validation-validatevaluecallback.html" rel="nofollow noreferrer">http://www.shujaat.net/2010/07/wpf-validation-validatevaluecallback.html</a></p></li> <li><p><a href="http://msdn.microsoft.com/en-us/library/ms745795.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms745795.aspx</a></p></li> <li><p><a href="http://wpf.2000things.com/2010/11/11/122-validating-a-dependency-property/" rel="nofollow noreferrer">http://wpf.2000things.com/2010/11/11/122-validating-a-dependency-property/</a></p></li> <li><p><a href="http://wpf.2000things.com/2010/11/12/123-coercing-a-dependency-property/" rel="nofollow noreferrer">http://wpf.2000things.com/2010/11/12/123-coercing-a-dependency-property/</a></p></li> <li><p><a href="http://wpf.2000things.com/2010/12/09/150-an-example-of-using-propertychanged-and-coercevalue-callbacks/" rel="nofollow noreferrer">http://wpf.2000things.com/2010/12/09/150-an-example-of-using-propertychanged-and-coercevalue-callbacks/</a></p></li> </ul> <p>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).</p> <pre><code>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; } </code></pre> <hr> <p>There are also different techniques you can use like ValidationRules, possibly MaskedTextBox, and using PreviewTextInput.</p> <ul> <li><p><a href="http://social.msdn.microsoft.com/Forums/en/wpf/thread/990c635a-c14e-4614-b7e6-65471b0e0e26" rel="nofollow noreferrer">http://social.msdn.microsoft.com/Forums/en/wpf/thread/990c635a-c14e-4614-b7e6-65471b0e0e26</a></p></li> <li><p><a href="https://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf">How do I get a TextBox to only accept numeric input in WPF?</a></p></li> <li><p><a href="http://weblogs.asp.net/monikadyrda/archive/2009/06/24/wpf-textbox-validation.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/monikadyrda/archive/2009/06/24/wpf-textbox-validation.aspx</a></p></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload