Restriction or Validation in WPF Double TextBox
18 Feb 20256 minutes to read
This section explains how to validate or restrict the DoubleTextBox control value.
Restrict the value within minimum and maximum value
The Value of the DoubleTextBox can be restricted within the maximum and minimum limits. Once the value has reached the maximum or minimum value , the value does not exceed the limit. We can change the maximum and minimum limits by using the MinValue property and MaxValue property.
You can choose when to validate the maximum and minimum limits while changing the values by using the MinValidation and MaxValidation properties.
-
OnKeyPress— When setting theMaxValidationorMinValidationtoOnKeyPress, the value in theDoubleTextBoxwill be validated shortly after pressing a key. So, it is not possible to provide any invalid input at all and the value does not exceed the maximum and minimum limits. -
OnLostFocus- When settingMaxValidationorMinValidationtoOnLostFocus, the value in theDoubleTextBoxis validated, when theDoubleTextBoxloses the focus. That is, theDoubleTextBoxwill accept any value, validation will only take place after theDoubleTextBoxhas lost its keyboard focus. After validation, when the value of theDoubleTextBoxis greater than theMaxValueor less than theMinValue, the value will be automatically set toMaxValueorMinValue. -
MaxValueOnExceedMaxDigit - When you give input greater than specified maximum limit,
MaxValueOnExceedMaxDigitproperty will decide either it should retain the old value or reset to maximum limit that is specified. For example, ifMaxValueis set to 100 and you are trying to input 200.Valuewill changed to 100 whenMaxValueOnExceedMaxDigitistrue. WhenMaxValueOnExceedMaxDigitisfalse, 20 will be retained and last entered 0 will be ignored.NOTE
MaxValueOnExceedMinDigitproperty will be enabled only when theMaxValidationis set toOnKeyPress. -
MinValueOnExceedMinDigit - When you give input less than specified minimum limit,
MinValueOnExceedMinDigitproperty will decide either it should retain the old value or reset to minimum limit that is specified. For example, ifMinValueis set to 200 and theValueis 205 and you are trying change the value to 20.Valuewill changed to 200 whenMinValueOnExceedMinDigitistrue. WhenMinValueOnExceedMinDigitisfalse, Old value 205 will be retained.NOTE
MinValueOnExceedMinDigitwill be enabled only when theMinValidationis set toOnKeyPress.
<syncfusion:DoubleTextBox x:Name="doubleTextBox" Width="150" MaxValue="100" MinValue="10"
MinValueOnExceedMinDigit="True" MaxValueOnExceedMaxDigit="True"
MinValidation="OnKeyPress" MaxValidation="OnLostFocus"/>DoubleTextBox doubleTextBox = new DoubleTextBox();
doubleTextBox.Width = 150;
doubleTextBox.Height = 25;
doubleTextBox.MinValue = 10;
doubleTextBox.MaxValue =100;
doubleTextBox.MinValidation = MinValidation.OnKeyPress;
doubleTextBox.MaxValidation = MaxValidation.OnLostFocus;
doubleTextBox.MinValueOnExceedMinDigit = true;
doubleTextBox.MaxValueOnExceedMaxDigit = true;MinValidation is set to OnKeyPress, it cannot let to enter a value less than the MinValue. If try to enter a value less than the MinValue, then the MinValue will set to the Value property because MinValueOnExceedMinDigit is set to true.

MaxValidation is set to OnLostFocus, so the MaxValidation will be performed only in the lost focus.

Restrict number of decimal digit
You can format the decimal digits in the DoubleTextBox control using NumberDecimalDigits property. You can restrict the decimal digits of the text within maximum and minimum limits in DoubleTextBox control using MinimumNumberDecimalDigits and MaximumNumberDecimalDigits properties. The default value of MinimumNumberDecimalDigits,MaximumNumberDecimalDigits and DoubleDecimalDigits properties is -1.
NOTE
If the value of
MinimumNumberDecimalDigitsproperty is greater than the value ofMaximumNumberDecimalDigitsproperty, the text ofDoubleTextBoxwill be updated based on the value ofMinimumNumberDecimalDigitsproperty.
<syncfusion:DoubleTextBox Value="125.32545" HorizontalAlignment="Center" VerticalAlignment="Center"
MaximumNumberDecimalDigits="4"
MinimumNumberDecimalDigits="1" />DoubleTextBox doubleTextBox = new DoubleTextBox();
doubleTextBox.Value = 125.32545;
doubleTextBox.MaximumNumberDecimalDigits = 4;
doubleTextBox.MinimumNumberDecimalDigits = 1;
When value of MinimumNumberDecimalDigits, MaximumNumberDecimalDigits and NumberDecimalDigits properties are specified, NumberDecimalDigits property takes high precedence and updates the text of DoubleTextBox property.
<syncfusion:DoubleTextBox Value="125.32545" HorizontalAlignment="Center" VerticalAlignment="Center"
MaximumNumberDecimalDigits="4"
MinimumNumberDecimalDigits="1"
NumberDecimalDigits="3"
/>DoubleTextBox doubleTextBox = new DoubleTextBox();
doubleTextBox.Value = 125.32545;
doubleTextBox.MaximumNumberDecimalDigits = 4;
doubleTextBox.MinimumNumberDecimalDigits = 1;
doubleTextBox.NumberDecimalDigits = 3;
Read only mode
The DoubleTextBox cannot allow the user input, edits when IsReadOnly property is sets to true. The user can still select text and display the cursor on the DoubleTextBox by setting the IsReadOnlyCaretVisible property to true. However, value can be changed programmatically in readonly mode.
<syncfusion:DoubleTextBox x:Name="doubleTextBox" IsReadOnly="True" Value="10" IsReadOnlyCaretVisible="True"/>DoubleTextBox doubleTextBox = new DoubleTextBox();
doubleTextBox.Value = 10;
doubleTextBox.IsReadOnly = true;
doubleTextBox.IsReadOnlyCaretVisible = true;