【发布时间】:2019-11-06 17:13:56
【问题描述】:
我有一个TextBox 绑定到具有[Required] 和[Range(1, 5)] 属性的int 属性,当应用程序启动时TextBox 得到0,如果我按Backspace 和删除那个 0,我在调试日志中收到这条消息:
System.Windows.Data Error: 7 : ConvertBack cannot convert value '' (type 'String'). BindingExpression:Path=Order.PartyCode; DataItem='OrderVM' (HashCode=66572856); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String') FormatException:'System.FormatException: Input string was not in a correct format.
at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at MS.Internal.Data.SystemConvertConverter.ConvertBack(Object o, Type type, Object parameter, CultureInfo culture)
at System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'
并且验证不起作用。如果我使用Nullable<int> 而不是int,我会得到同样的错误!
必须拥有string 属性才能使用TextBox 还是有办法解决这个问题?
【问题讨论】:
标签: c# wpf validation data-binding idataerrorinfo