-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!

9
  • 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. Commented May 7, 2020 at 15:33
  • Added button click handler code and image for clarification Commented May 7, 2020 at 15:47
  • Where do you bind this DependencyProperty to in the xaml? Commented May 7, 2020 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? Commented May 7, 2020 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 Commented May 7, 2020 at 16:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.