I am having an issue with a custom control dependency property. The control is basically a textbox with a dropdown numpad.
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!