Restriction or Validation in WPF Currency TextBox
18 Nov 20186 minutes to read
This section explains how to validate or restrict the WPF Currency TextBox control value.
Restrict the value within minimum and maximum value
The Value of the WPF Currency TextBox 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. You can change the maximum and minimum limits by using the MinValue and MaxValue properties. The default validation mode for MinValidation and MaxValidation is OnLostFocus.
You can choose when to validate the maximum and minimum limits while changing the values by using the MinValidation and MaxValidation properties.
-
OnKeyPress— WhenMaxValidationorMinValidationis set toOnKeyPress, the value in theWPF Currency TextBoxis validated shortly after each key press. As a result, it is not possible to enter any invalid input and the value will never exceed the configured maximum or minimum limits. -
OnLostFocus- WhenMaxValidationorMinValidationis set toOnLostFocus, the value in theWPF Currency TextBoxis validated when the control loses keyboard focus. TheWPF Currency TextBoxwill accept any value during editing; validation takes place only after focus is lost. If the value is then greater thanMaxValueor less thanMinValue, it is automatically reset toMaxValueorMinValue, respectively. -
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.MaxValueOnExceedMaxDigitproperty takes effect 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.MinValueOnExceedMinDigittakes effect only when theMinValidationis set toOnKeyPress.
<syncfusion:CurrencyTextBox x:Name="currencyTextBox" Width="150" MaxValue="100" MinValue="10"
MinValueOnExceedMinDigit="True"
MinValidation="OnKeyPress" MaxValidation="OnLostFocus"/>CurrencyTextBox currencyTextBox = new CurrencyTextBox();
currencyTextBox.Width = 150;
currencyTextBox.Height = 25;
currencyTextBox.MinValue = 10;
currencyTextBox.MaxValue =100;
currencyTextBox.MinValidation = MinValidation.OnKeyPress;
currencyTextBox.MaxValidation = MaxValidation.OnLostFocus;
currencyTextBox.MinValueOnExceedMinDigit = 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 Digits
You can format the decimal digits in the WPF Currency TextBox control using CurrencyDecimalDigits property. You can also restrict the decimal digits of the text within minimum and maximum limit in WPF Currency TextBox control using MinimumCurrencyDecimalDigits and MaximumCurrencyDecimalDigits properties. The default value of MinimumCurrencyDecimalDigits,MaximumCurrencyDecimalDigits and CurrencyDecimalDigits properties is -1.
If the value of
MinimumCurrencyDecimalDigitsproperty is greater than the value ofMaximumCurrencyDecimalDigitsproperty, the text ofWPF Currency TextBoxwill be updated based on value ofMinimumCurrencyDecimalDigitsproperty.
<syncfusion:CurrencyTextBox Value="125.32545" HorizontalAlignment="Center" VerticalAlignment="Center"
MaximumCurrencyDecimalDigits="4"
MinimumCurrencyDecimalDigits="1" />CurrencyTextBox currencyTextBox = new CurrencyTextBox();
currencyTextBox.Value = 125.32545;
currencyTextBox.MaximumCurrencyDecimalDigits = 4;
currencyTextBox.MinimumCurrencyDecimalDigits = 1;
When the value of MinimumCurrencyDecimalDigits, MaximumCurrencyDecimalDigits and CurrencyDecimalDigits properties are specified, CurrencyDecimalDigits property takes high precedence and updates the text of WPF Currency TextBox property.
<syncfusion:CurrencyTextBox Value="125.32545" HorizontalAlignment="Center" VerticalAlignment="Center"
MaximumCurrencyDecimalDigits="4"
MinimumCurrencyDecimalDigits="1"
CurrencyDecimalDigits="3"
/>CurrencyTextBox currencyTextBox = new CurrencyTextBox();
currencyTextBox.Value = 125.32545;
currencyTextBox.MaximumCurrencyDecimalDigits = 4;
currencyTextBox.MinimumCurrencyDecimalDigits = 1;
currencyTextBox.CurrencyDecimalDigits = 3;
Read-Only Mode
The WPF Currency TextBox does not allow the user to input or edit the value when the IsReadOnly property is set to true. The user can still select text and display the cursor on the WPF Currency TextBox by setting the IsReadOnlyCaretVisible property to true. However, the value can still be changed programmatically in read-only mode.
<syncfusion:CurrencyTextBox x:Name="currencyTextBox" IsReadOnly="True" Value="10" IsReadOnlyCaretVisible="True"/>CurrencyTextBox currencyTextBox = new CurrencyTextBox();
currencyTextBox.Value = 10;
currencyTextBox.IsReadOnly = true;
currencyTextBox.IsReadOnlyCaretVisible = true;