当属性抛出错误时,如何将其设置为空值?
WPF 中的绑定非常强大。假设我们有一个 Number 属性(可为 null int)并且绑定到一个文本框。
我意识到当它抛出错误时,该属性具有最后的正确值。
我的意思是这些过程:
TEXTBOX: "" PROPERTY: null
TEXTBOX: "2" PROPERTY: 2
TEXTBOX: "2b" PROPERTY: 2 <-- here is the problem, should be null instead 2(by the error)
有没有一种方法可以让绑定在产生错误时设置空值?
有些人告诉我我需要实现 IDataErrorInfo,但我想该接口是为了验证业务规则。所以我不喜欢使用它。
更新:
<TextBox Text="{Binding Number, UpdateSourceTrigger=PropertyChanged,
ValidatesOnExceptions=True, ValidatesOnDataErrors=True,
NotifyOnValidationError=True, TargetNullValue={x:Static sys:String.Empty}}"
Binding is so powerful in WPF. Supposed that we have a Number property (nullable int) and is bound to a textbox.
I realized when it throws an error, the property has the last correct value.
I mean these are the processes:
TEXTBOX: "" PROPERTY: null
TEXTBOX: "2" PROPERTY: 2
TEXTBOX: "2b" PROPERTY: 2 <-- here is the problem, should be null instead 2(by the error)
Is there a way which the binding set a null value when it produce an error?
Some persons told me I need to implement IDataErrorInfo, but I guess that interface is to validate business rules. So I wouldn't prefer user it.
UPDATE:
<TextBox Text="{Binding Number, UpdateSourceTrigger=PropertyChanged,
ValidatesOnExceptions=True, ValidatesOnDataErrors=True,
NotifyOnValidationError=True, TargetNullValue={x:Static sys:String.Empty}}"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用
UpdateSourceTrigger=PropertyChanged
,这意味着只要用户按下某个键,它就会将数据存储在您的数据上下文中。例如,用户输入
2
,然后输入您的属性等于“2”
。用户输入b
,它将尝试用"2b"
替换"2"
,但失败,因此" 的原始属性2"
仍然存在。删除
UpdateSourceTrigger
,它将恢复为默认的LostFocus
,这意味着它只会在 TextBox 失去焦点时更新属性。当产生错误时,您可以将该属性设置为
null
,但我不建议这样做,因为如果用户不小心按错了键,TextBox
将被清除。附带说明一下,请使用
IDataErrorInfo
进行所有验证,而不仅仅是业务规则验证。 WPF 就是为了与它一起工作而构建的。我的模型使用它来验证其数据的长度、类型等是否正确,我的 ViewModel 使用它来验证是否遵循业务规则编辑
我的替代建议是绑定到字符串值,而不是数字字段。这样,当值发生变化时,您可以尝试将其转换为 Int,如果无法转换则返回错误。
You are using
UpdateSourceTrigger=PropertyChanged
, which means that anytime the user hits a key, it is storing the data in your data contextFor example, user types
2
, then your property is equal to"2"
. User typesb
and it will attempt to replace"2"
with"2b"
, which fails, so the original property of"2"
remains.Remove the
UpdateSourceTrigger
and it will revert to the default ofLostFocus
, which means it will only update the property when the TextBox loses focus.You could set the property to
null
when an error is produced, but I would not recommend doing that because then if a user accidently hits the wrong key, theTextBox
would get cleared.As a side note, use
IDataErrorInfo
for all validation, not just business rule validation. WPF is built to work with it. My Models use it to verify their data is the correct length, type, etc, and my ViewModels use it to verify that business rules are being followedEdit
The alternative suggestion I would have would be to bind to a string value, not a number field. This way when the value changes, you can try and cast it to your Int and return an error if it can't be cast.
我认为获得该行为的最简单方法是使用
IValueConverter
将string
转换为int?
:然后您可以在您的绑定如下(其中
local
映射到您的转换器命名空间):I think that the simplest way to get that behaviour is to use an
IValueConverter
to convert fromstring
toint?
:Then you can specify this in your binding as below (where
local
is mapped to your converter namespace):它变得更加强大。您可能应该通过接口/绑定本身进行验证 - WPF 对此有内置支持,其示例可以在 MSDN 上的数据绑定概述。
实现此操作的示例如下:
链接的文档涵盖了有关绑定主题的大量内容,因此这里是相关部分“数据验证”的摘录:
It gets more powerful yet. You probably ought to go the route of validation via the interface/binding itself - WPF has built-in support for this, examples of which can be found in the Data Binding Overview over at MSDN.
An example of implementing this could go as follows:
The linked documentation covers quite a bit on the topic of binding, so here is an excerpt from the relevant section 'Data Validation':