Restriction or Validation in WPF Percent TextBox
18 Feb 20256 minutes to read
This section explains how to validate or restrict the PercentTextBox control value.
Restrict the value within minimum and maximum value
The PercentValue of the PercentTextBox can be restricted within the maximum and minimum limits. Once the percent 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 percent values by using the MinValidation and MaxValidation properties.
-
OnKeyPress— When setting theMaxValidationorMinValidationtoOnKeyPress, the percent value in thePercentTextBoxwill be validated shortly after pressing a key. So, it is not possible to provide any invalid input at all and the percent value does not exceed the maximum and minimum limits. -
OnLostFocus- When settingMaxValidationorMinValidationtoOnLostFocus, the percent value in thePercentTextBoxis validated, when thePercentTextBoxloses the focus. That is, thePercentTextBoxwill accept any percent value, validation will only take place after thePercentTextBoxhas lost its keyboard focus. After validation, when the percent value of thePercentTextBoxis greater than theMaxValueor less than theMinValue, the percent 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 percent value or reset to maximum limit that is specified. For example, ifMaxValueis set to 100 and you are trying to input 200.PercentValuewill 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 percent value or reset to minimum limit that is specified. For example, ifMinValueis set to 200 and thePercentValueis 205 and you are trying change the percent value to 20.PercentValuewill changed to 200 whenMinValueOnExceedMinDigitistrue. WhenMinValueOnExceedMinDigitisfalse, Old percent value 205 will be retained.NOTE
MinValueOnExceedMinDigitwill be enabled only when theMinValidationis set toOnKeyPress.
<syncfusion:PercentTextBox x:Name="percentTextBox" Width="150" MaxValue="100" MinValue="10"
MinValueOnExceedMinDigit="True" MaxValueOnExceedMaxDigit="True"
MinValidation="OnKeyPress" MaxValidation="OnLostFocus"/>PercentTextBox percentTextBox = new PercentTextBox();
percentTextBox.Width = 150;
percentTextBox.Height = 25;
percentTextBox.MinValue = 10;
percentTextBox.MaxValue =100;
percentTextBox.MinValidation = MinValidation.OnKeyPress;
percentTextBox.MaxValidation = MaxValidation.OnLostFocus;
percentTextBox.MinValueOnExceedMinDigit = true;
percentTextBox.MaxValueOnExceedMaxDigit = true;MinValidation is set to OnKeyPress, it cannot let to enter a percent value less than the MinValue. If try to enter a percent value less than the MinValue, then the MinValue will set to the PercentValue 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 PercentTextBox control using PercentDecimalDigits property. You can restrict the decimal digits of text within maximum and minimum limits in PercentTextBox control using MinPercentDecimalDigits and MaxPercentDecimalDigits properties. The default value of MinPercentDecimalDigits,MaxPercentDecimalDigits and DoubleDecimalDigits properties is -1.
NOTE
If the value of
MinPercentDecimalDigitsproperty is greater than value of theMaxPercentDecimalDigitsproperty, the text ofPercentTextBoxwill be updated based on value ofMinPercentDecimalDigitsproperty.
<syncfusion:PercentTextBox Value="125.32545" HorizontalAlignment="Center" VerticalAlignment="Center"
MaxPercentDecimalDigits="4"
MinPercentDecimalDigits="1" />PercentTextBox percentTextBox = new PercentTextBox();
percentTextBox.Value = 125.32545;
percentTextBox.MaxPercentDecimalDigits = 4;
percentTextBox.MinPercentDecimalDigits = 1;
When the value of MinPercentDecimalDigits, MaxPercentDecimalDigits and PercentDecimalDigits properties are specified, PercentDecimalDigits property takes high precedence and updates the text of PercentTextBox property.
<syncfusion:PercentTextBox Value="125.32545" HorizontalAlignment="Center" VerticalAlignment="Center"
MaxPercentDecimalDigits="4"
MinPercentDecimalDigits="1"
PercentDecimalDigits="3"
/>PercentTextBox percentTextBox = new PercentTextBox();
percentTextBox.Value = 125.32545;
percentTextBox.MaxPercentDecimalDigits = 4;
percentTextBox.MinPercentDecimalDigits = 1;
percentTextBox.PercentDecimalDigits = 3;
Read only mode
The PercentTextBox 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 PercentTextBox by setting the IsReadOnlyCaretVisible property to true. However, value can be changed programmatically in readonly mode.
<syncfusion:PercentTextBox x:Name="percentTextBox" IsReadOnly="True" PercentValue="10" IsReadOnlyCaretVisible="True"/>PercentTextBox percentTextBox = new PercentTextBox();
percentTextBox.PercentValue = 10;
percentTextBox.IsReadOnly = true;
percentTextBox.IsReadOnlyCaretVisible = true;