wpf idataerrorinfo 组合框
我有一个简单的视图和视图模型,后者实现 IDdataErrorInfo 和 IValidationExceptionHandler。该视图包含一个组合框,其 ItemsSource 绑定到一个集合,而 SelectedValue 绑定到一个属性 - 验证数据错误和异常;更新属性更改并通知源更新。
我正在使用数据注释,并且该属性用“Required”和“Range”属性“装饰”,并且在执行视图时我希望组合框显示错误装饰器以及我为“Required”属性提供的消息。但是,将鼠标悬停在装饰器上时,我注意到消息是“值”无法转换”。
如果我发现没有选择任何值,是否必须检查属性的 getter 中选定的值并在消息中返回 ValidationResult?我认为仅使用必需属性就可以解决问题,但这似乎不适用于组合框?
非常感谢任何帮助。
I have a simple view and viewmodel with the latter implementing IDdataErrorInfo and IValidationExceptionHandler. The view contains a combobox with its ItemsSource bound to an collection with SelectedValue bound to a property - validating on data errors and exceptions; updates on property changed and notifies on source updated.
I'm using data annotations and the property is "decorated" with Required as well as a Range attribute and I would upon executing the view have expected the combobox to display the error adorner and the message I've provided for the Required attribute. However, hovering over the adorner I notice the message is instead "Value "" could not be converted".
Do I have to check the selected value in the getter for the property and return a ValidationResult with my message if I find that no value has been selected? I thought just using the Required attribute would have done the trick but that doesn't seem to work for comboboxes?
Any help much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果组合框中没有代表 SelectedValue 所代表类型的默认值的项目,通常会发生这种情况。
假设您绑定的选定值属性是字符串,则应该有一个项目(在 ItemsSource 中)表示 string.empty 的默认值。
因此(可能)错误是 Value ""无法转换
让我知道这是否有帮助。
This usually happes if there is no representing item in the combobox for the default value of what type the SelectedValue represents.
Assuming that your bound selected value property is string there should be an item (in the ItemsSource) which represents the default value of string.empty.
And hence (probably) the error is Value "" could not be converted
Let me know if this helps.