Restriction or Validation in WPF Percent TextBox
18 Nov 20186 minutes to read
This section explains how to validate or restrict the WPF Percent TextBox control value.
Restrict the value within minimum and maximum value
The PercentValue of the WPF Percent TextBox can be restricted within the maximum and minimum limits. Once the percent value has reached the maximum or minimum value, the value will not exceed the limit. You can change the maximum and minimum limits by using the MinValue and MaxValue properties.
You can choose when to validate the maximum and minimum limits while changing the percent values by using the MinValidation and MaxValidation properties. The default value of both properties is OnKeyPress.
-
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 will not exceed the maximum and minimum limits. -
OnLostFocus- WhenMaxValidationorMinValidationis set toOnLostFocus, the percent value is validated when theWPF Percent TextBoxloses focus. The control accepts any percent value while it has focus, and validation occurs only after focus is lost. If the value exceedsMaxValueor is less thanMinValue, it is automatically reset toMaxValueorMinValue, respectively. -
MaxValueOnExceedMaxDigit - When you give input greater than the specified maximum limit, the
MaxValueOnExceedMaxDigitproperty will decide whether to retain the old percent value or reset to the maximum limit. For example, ifMaxValueis set to 100 and you are trying to input 200,PercentValuewill be changed to 100 whenMaxValueOnExceedMaxDigitistrue. WhenMaxValueOnExceedMaxDigitisfalse, 20 will be retained and the last entered 0 will be ignored.MaxValueOnExceedMaxDigitproperty is enabled only when theMaxValidationis set toOnKeyPress. -
MinValueOnExceedMinDigit - When you give input less than the specified minimum limit, the
MinValueOnExceedMinDigitproperty will decide whether to retain the old percent value or reset to the minimum limit. For example, ifMinValueis set to 200 and thePercentValueis 205 and you are trying to change the percent value to 20,PercentValuewill be changed to 200 whenMinValueOnExceedMinDigitistrue. WhenMinValueOnExceedMinDigitisfalse, the old percent value 205 will be retained.MinValueOnExceedMinDigitis 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, so it will not allow entering a percent value less than the MinValue. If you try to enter a percent value less than the MinValue, then the MinValue will be set to the PercentValue property because MinValueOnExceedMinDigit is set to true.

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

Restrict number of decimal digits
You can format the decimal digits in the PercentTextBox control using the PercentDecimalDigits property. You can restrict the decimal digits of the text within maximum and minimum limits in the PercentTextBox control using the MinPercentDecimalDigits and MaxPercentDecimalDigits properties. The default value of the MinPercentDecimalDigits, MaxPercentDecimalDigits, and DoubleDecimalDigits properties is -1 (unrestricted). DoubleDecimalDigits controls the decimal digits used to display the underlying double value when the control is not focused.
If the value of the
MinPercentDecimalDigitsproperty is greater than the value of theMaxPercentDecimalDigitsproperty, the text ofWPF Percent TextBoxwill be updated based on the value of theMinPercentDecimalDigitsproperty.
<syncfusion:PercentTextBox PercentValue="125.32545" HorizontalAlignment="Center" VerticalAlignment="Center"
MaxPercentDecimalDigits="4"
MinPercentDecimalDigits="1" />PercentTextBox percentTextBox = new PercentTextBox();
percentTextBox.PercentValue = 125.32545;
percentTextBox.MaxPercentDecimalDigits = 4;
percentTextBox.MinPercentDecimalDigits = 1;
When the values of MinPercentDecimalDigits, MaxPercentDecimalDigits, and PercentDecimalDigits are specified, the PercentDecimalDigits property takes higher precedence and updates the text of the WPF Percent TextBox property.
<syncfusion:PercentTextBox PercentValue="125.32545" HorizontalAlignment="Center" VerticalAlignment="Center"
MaxPercentDecimalDigits="4"
MinPercentDecimalDigits="1"
PercentDecimalDigits="3"
/>PercentTextBox percentTextBox = new PercentTextBox();
percentTextBox.PercentValue = 125.32545;
percentTextBox.MaxPercentDecimalDigits = 4;
percentTextBox.MinPercentDecimalDigits = 1;
percentTextBox.PercentDecimalDigits = 3;
Read only mode
The WPF Percent TextBox does not allow user input or edits when the IsReadOnly property is set to true. The user can still select text and display the cursor on the WPF Percent TextBox by setting the IsReadOnlyCaretVisible property to true. However, the value can be changed programmatically in read-only 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;