会计编号的文本框

TextBox for accounting number

在我的 class 我有一个 Number 属性

double number;
public double Number { get => number; set { number = value; OnPropertyChanged(); } }

以这种方式绑定到 xaml 中的 TextBox

<TextBox Text="{Binding Number, UpdateSourceTrigger=PropertyChanged, StringFormat=N2}"/>

当我启动应用程序时,TextBox 会自动获取 0.00,可能是因为默认情况下初始化了值类型。如果我 TABTextBox 焦点,它不会 select 0.00 所以我必须手动删除第一个 0, 在小数点前,输入一些值。如果我输入一个整数,我会得到预期的尾随 .00 但是如果我在数字键盘中按 . 在整数部分后添加一些小数点,我会得到一个红色装饰TextBox 周围和 .00 不会被删除!因此,我不必按 . 并输入数字,而是先按 以获取小数部分,然后再输入数字。

我希望 TextBox 在应用程序启动时和我点击 时 blank/null。 我希望尾随 .00替换为我输入的任何内容,即。 .25,此后如果我按下一个按钮,要处理代码中的值,我希望 TextBox 再次成为 blank/null!怎么做?

有没有屏蔽的TextBox可以达到目的?

A double 是永远不可能是 null 的值类型。如果您希望 TextBlock 为空,您应该绑定到默认值为 nullNullable<double>

@mm8, that's great, it solves half of the problem and if one gets used to press right arrow instead of . for values after decimal point, it actually solves the whole problm. Is there any chance of getting unexpected value like this with Nullable<double>?

不可以,Nullable<double> 属性 只能设置为 double 值或 null。没有其他的。换句话说,它接受与 doublenull (default(double?).

完全相同的值