WPF DependencyProperty问题,后缀“。”

问题描述 投票:-1回答:1

我在使用自定义控件依赖项属性时遇到问题。该控件基本上是一个带有下拉数字小键盘的文本框。

enter image description here

当用户按下数字键盘上的按钮时,文本将附加到InputValue依赖项属性。一切正常,直到我尝试附加“。”为止。到属性值。集合中的“值”变量仍带有“”。 (例如:“ 1。”),但是在SetValue之后,InputValue没有“。”。 (例如:“ 1”)

代码如下-OnInputValueChanged只是检查控件是否为特定类型的输入,如果是,则在字符串达到特定长度时,关闭数字键盘。

    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();
            }
    }

如果替换按十进制按钮的代码以添加诸如“ .0”之类的不同内容,我将以“ 1.0”结尾,但是一旦返回到尾随的0,小数又消失了。

按钮单击处理程序代码为:

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

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

重复0-9个按钮

Xaml绑定:

        <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" />

在我看来,值正在转换为某个地方的数字,并删除训练“。”。

任何建议将不胜感激!

c# wpf dependency-properties
1个回答
0
投票
一种方法是将UpdateSourceTrigger延迟到LostFocus,但我认为这是不可能的,因为您将其辅助在后面的代码上。

因此,您唯一的选择是不将其绑定到数字类型并保持其为字符串类型。

© www.soinside.com 2019 - 2024. All rights reserved.