-1

I am having an issue with a custom control dependency property. The control is basically a textbox with a dropdown numpad.

enter image description here

When the user presses a button on the numpad, the text is appended to the InputValue dependency property. Everything works as expected until I try to append a "." to the property value. The "value" variable in the set still has the "." (ex: "1.") but after the SetValue, InputValue doesn't have the "." (ex: "1")

The code is as follows - OnInputValueChanged is simply checking to see if the control is a specific kind of input and if so, when the string gets to a certain length, close the numpad.

    public static readonly DependencyProperty InputValueProperty = DependencyProperty.Register("InputValue", typeof(String), typeof(TextBoxTouchNum), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsParentArrange, new PropertyChangedCallback(OnInputValueChanged)));
    public String InputValue
    {
        get => (String)GetValue(InputValueProperty);
        set
        {
            SetValue(InputValueProperty, value);
        }
    }

    public static void OnInputValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TextBoxTouchNum ctrl = (TextBoxTouchNum)d;
        String val = (String)ctrl.InputValue;

        if (val != null && ctrl.IsHeatNoEntry && val.Trim().Length == 8)
            if (ctrl.IsTouchScreen && ctrl.NumPadVisible)
            {
                ctrl.NumPadVisible = false;
                Keyboard.ClearFocus();
            }
    }

If I replace the code for the decimal button press to append something different like ".0", I end up with "1.0" but as soon as I back over the trailing 0, the decimal is gone again.

The button click handler code is:

    private void Decimal_Click(object sender, RoutedEventArgs e)
    {
        InputValue += ".";
    }

    private void Num0_Click(object sender, RoutedEventArgs e)
    {
        InputValue += "0";
    }

repeated for 0-9 buttons

Xaml Binding:

        <TextBox x:Name="TextBoxValue" 
             Width="{Binding TextBoxWidth}" 
             Text="{Binding InputValue, RelativeSource={RelativeSource AncestorType=UserControl}}"
             materialDesign:HintAssist.Hint="{Binding Hint}"
             Style="{StaticResource MaterialDesignFloatingHintTextBox}" 
             TextAlignment="Center"
             VerticalAlignment="Center" 
             Background="Transparent" />

It seems to me that the value is being converted to a numeric somewhere and dropping the training ".".

Any suggestions would be appreciated!

CihSoft
  • 21
  • 5
  • My guess is that your dropdown values are numbers without the dot. Show us the dropdown codes, and also how you are setting the value. It will help to determine what went wrong. – kurakura88 May 07 '20 at 15:33
  • Added button click handler code and image for clarification – CihSoft May 07 '20 at 15:47
  • Where do you bind this DependencyProperty to in the xaml? – kurakura88 May 07 '20 at 15:58
  • From the code and information you posted it seems obvious that the modification happens when binding `InputValue` I guess to `TextBox.Text`. Are there any converters or attached properties/behaviors involved? What is happening inside `OnInputValueChanged`? – BionicCode May 07 '20 at 16:03
  • Also, if I enter the number from the keyboard rather than the onscreen numpad and leave a trailing dot, it removes it then too – CihSoft May 07 '20 at 16:14
  • Also, interesting to note, if the dot is the first character in the property, it works correctly but a leading 0 is added – CihSoft May 07 '20 at 16:21
  • Try remove the styling and see if that works? If so, the styling might have validation kicks in every PropertyChanged. When you bind that custom control value to number type, it will reformat to proper number instantly. Therefore an option is to change the UpdateSourceTrigger to LostFocus or change the binding to string type (no number validation). – kurakura88 May 07 '20 at 16:31
  • some reference https://stackoverflow.com/questions/21004951/wpf-validation-rule-preventing-decimal-entry-in-textbox – kurakura88 May 07 '20 at 16:34
  • kurakura88 - it ended up being the datatype coming from the parent...If you put your comment out there as a proposed answer I'll accept it. – CihSoft May 07 '20 at 16:36

1 Answers1

0

If that dependency property is bound to numeric type and UpdateSourceTrigger is set to PropertyChanged, entering dot(decimal sign) will immediately set the property and get property will be called back to the UI resulting to no dot(decimal sign) visible.

One way is to delay the UpdateSourceTrigger to LostFocus, but I don't think this is possible, because you are assigining it on the code behind.

So your only option is not to bind it to numeric type and keep it in string type.

kurakura88
  • 2,185
  • 2
  • 12
  • 18