Telerik RadMaskedNumericInput 允许空值

Telerik RadMaskedNumericInput allow empty value

我的目标是得到一个 MaskedNumericInput 框,其中允许 0-9999 之间的数字。它的值被绑定到一个整数。

<telerik:RadMaskedNumericInput Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WpfExt:RadGridViewExt}}, Path=DataContext.MaxInvoicesPerConcatenationFile, Mode=TwoWay}"
                               Mask="#4"  UpdateValueEvent="LostFocus" TextMode="PlainText"
                               maskedInput:MaskedInputExtensions.Maximum="9999"/>

我没有使用任何类型的验证器或其他东西。似乎有一个内部验证器。如果我清除我的框,它将以红色突出显示并显示错误消息:

"Value "" could not be converted."

有什么方法可以让它使用空值吗?

有什么办法可以让输入为空吗?

你可以将AllowNull附加属性设置为False,然后错误消失,值0将设置为绑定属性 清除屏蔽输入框时,虽然显示为空

<telerik:RadMaskedNumericInput Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WpfExt:RadGridViewExt}}, Path=DataContext.MaxInvoicesPerConcatenationFile, Mode=TwoWay}"
                               Mask="#4"  UpdateValueEvent="LostFocus" TextMode="PlainText"
                               maskedInput:MaskedInputExtensions.Maximum="9999"
                               maskedInput:MaskedInputExtensions.AllowNull="False"/>

与文档相反,AllowNull 属性 默认设置为 true,而不是 false。这样,当清除输入时,该值将设置为 null,但由于类型 int 的绑定 属性 不能是 null,您将收到此错误。来自 AllowNull 的文档:

By default the RadMaskedNumericInput and RadMaskedCurrencyInput don't allow you to set null to their Value property. Instead the null value is coerced to 0. To alter this behavior and allow null values you can set the MaskedInputExtensions.AllowNull attached property to True.

也就是说,另一种方法是将您的 属性 更改为可空类型 int?,这将在清除输入框时设置 null 而不会出现任何错误显示,但您必须处理视图模型中的 null 值。哪种方法最适合取决于您的要求。