Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Textbox Value Doesn't Include Decimal Digit 0
    primarykey
    data
    text
    <p>In the KeyUp event of a WPF textbox, the value of textBox.Text is correct, unless the user enters a 0. For example, if the user enters 5.88, the textBox.Text value is "5.88". If the user enters 5.80, the textBox.Text value is "5.8" (the 0 is dropped). We're only allowing the user to enter one decimal digit, so, if they enter 5.80, we want to trim the 0. The problem is that we can't, because the code that does the trimming, only sees "5.8". When the operation is done, "5.80" still appears in the textbox for the user to see.</p> <p>Any idea why this would happen?</p> <p>Note: There is a converter applied to the textbox, but the KeyUp event sets the value to textBox.Text. So, if the converter produces 5.8, the textBox.Text value gets set to "5.8".</p> <p>Edit: Here is some of the code:</p> <pre><code>&lt;Window.Resources&gt; &lt;converters:StringToBooleanConverter x:Key="stringToBooleanConverter" /&gt; &lt;converters:SecondsToMinutesConverter x:Key="secondsToMinutesConverter" /&gt; &lt;/Window.Resources&gt; &lt;TextBox Text="{Binding ApplyTimeInSeconds, Converter={StaticResource secondsToMinutesConverter}, TargetNullValue={x:Static sys:String.Empty}, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnExceptions=True}" vab:Validate.BindingForProperty="Text" Name="ApplyTimeTextBox" KeyUp="ApplyTimeTextBox_KeyUp" Width="75" VerticalAlignment="Center" Style="{StaticResource textBoxInError}" IsEnabled="{Binding ElementName=applyTimeCheckbox, Path=IsChecked}"/&gt; private void ApplyTimeTextBox_KeyUp(object sender, KeyEventArgs e) { ViewUtility.RemoveExtraDecimalDigits(sender as TextBox, 1); } // We don't want to allow more than one decimal position. So, if the user types 4.38, remove the 8. Or change 4.389 to 4.3. internal static void RemoveExtraDecimalDigits(TextBox textBox, int numberOfDecimalDigitsAllowed) { if (!textBox.Text.Contains(".")) { return; } string originalText = textBox.Text; textBox.Text = GetValueWithCorrectPrecision(textBox.Text, numberOfDecimalDigitsAllowed); // If the text changed, move the cursor to the end. If there was no change to make, or maybe the user hit the // HOME key, no reason to move the cursor. if (textBox.Text != originalText) { MoveCursorToEnd(textBox); } } private static string GetValueWithCorrectPrecision(string textValue, int numberOfDecimalDigitsAllowed) { int indexOfDecimalPoint = textValue.IndexOf('.'); string[] numberSection = textValue.Split('.'); if (numberSection[1].Length &gt; numberOfDecimalDigitsAllowed) { // Keep the decimal point and the desired number of decimal digits (precision) return textValue.Remove(indexOfDecimalPoint + numberOfDecimalDigitsAllowed + 1); } return textValue; } private static void MoveCursorToEnd(TextBox textBox) { textBox.Select(textBox.Text.Length, 0); // Keep cursor at end of text box } </code></pre> <p>And here is the converter:</p> <pre><code>public class SecondsToMinutesConverter : IValueConverter { #region IValueConverter Members /// &lt;summary&gt; /// Converts a value from the source (domain object/view model) to the target (WPF control). /// &lt;/summary&gt; /// &lt;param name="value"&gt;The value produced by the binding source.&lt;/param&gt; /// &lt;param name="targetType"&gt;The type of the binding target property.&lt;/param&gt; /// &lt;param name="parameter"&gt;The converter parameter to use.&lt;/param&gt; /// &lt;param name="culture"&gt;The culture to use in the converter.&lt;/param&gt; /// &lt;returns&gt; /// A converted value. If the method returns null, the valid null value is used. /// &lt;/returns&gt; public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if(value == null) { return null; } decimal bindingSourceValueAsDecimal = System.Convert.ToDecimal(value, CultureInfo.CurrentCulture); return Decimal.Round(bindingSourceValueAsDecimal / 60, 2); } /// &lt;summary&gt; /// Converts a value from the target (WPF control) to the source (domain object/view model). /// &lt;/summary&gt; /// &lt;param name="value"&gt;The value that is produced by the binding target.&lt;/param&gt; /// &lt;param name="targetType"&gt;The type to convert to.&lt;/param&gt; /// &lt;param name="parameter"&gt;The converter parameter to use.&lt;/param&gt; /// &lt;param name="culture"&gt;The culture to use in the converter.&lt;/param&gt; /// &lt;returns&gt; /// A converted value. If the method returns null, the valid null value is used. /// &lt;/returns&gt; public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if(value == null) { return null; } decimal bindingTargetValueAsDecimal; if(Decimal.TryParse(value.ToString(), out bindingTargetValueAsDecimal) == false) { return DependencyProperty.UnsetValue; } return Math.Round(bindingTargetValueAsDecimal * 60, 2); } #endregion } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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