简体   繁体   English

如何捕获组合框SelectedValue绑定引发的FormatException

[英]How to catch FormatException thrown by a combobox SelectedValue binding

I have a combobox with a simple binding: 我有一个带有简单绑定的组合框:

SelectedValue="{Binding Path=SomeValue, Mode=TwoWay}

The problem is that SomeValue is sometimes a string not parsable to int. 问题在于SomeValue有时是无法解析为int的字符串。 And I get " System.FormatException: Input string was not in a correct format. " as soon as I set the ItemsSource. 设置ItemsSource后,我得到“ System.FormatException:输入字符串的格式不正确。

First question: how to handle the exception since I get a error message box. 第一个问题:自从出现错误消息框以来,如何处理异常。 Second question: can I show kind of a validation error to the user? 第二个问题:我可以向用户显示一种验证错误吗?

Attention: the source of the problem comes from the view model data source so I don't think it has anything to classical validation patterns. 注意:问题的根源来自视图模型数据源,因此我认为经典验证模式没有任何意义。

Create a dummy converter and put it into the debugger and put a breakpoint into it. 创建一个虚拟转换器,并将其放入调试器,并在其中插入一个断点。 It will become obvious what is wrong, types will be different. 哪里出了问题会很明显,类型也会有所不同。 Use Snoop to determine the error logs by holding control shift and selecting that element and look under SelectedValue property. 使用Snoop通过按住控制键并选择该元素并在SelectedValue属性下查看来确定错误日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM