C# 文本框输入错误后清空有效值-WPF
因此,我有一个包含文本框和标签的简单视图C# 文本框输入错误后清空有效值-WPF,c#,wpf,validation,mvvm,data-binding,C#,Wpf,Validation,Mvvm,Data Binding,因此,我有一个包含文本框和标签的简单视图 <TextBox x:Name="MyIntTextBox" Text="{Binding MyInt, UpdateSourceTrigger=LostFocus, Mode=TwoWay, StringFormat={}{D2}}"/> <Label Content="{Binding MyStr2}"/> 简单地说,文本框文本绑定到一个十进制值,标签应该以正确的格式显示文本框中的值 由于我在文本框中有Los
<TextBox x:Name="MyIntTextBox" Text="{Binding MyInt, UpdateSourceTrigger=LostFocus, Mode=TwoWay, StringFormat={}{D2}}"/>
<Label Content="{Binding MyStr2}"/>
简单地说,文本框
文本
绑定到一个十进制
值,标签
应该以正确的格式显示文本框
中的值
由于我在文本框中有LostFocus
作为我的UpdateSourceTrigger
,我按下选项卡
,以便验证
和绑定
工作
因此,当我输入一个decimal
值时,一切正常。标签
正确显示格式化的数字
当我输入一些垃圾非十进制值时,文本框
边框
变为红色,表示验证错误
但是,当我在后面加上一些有效值时,就像这样
…然后将注意力从文本框中移开,砰!文本框变为空白
然而,标签
指示正确的值。我在ViewModel
中设置了断点,我可以看到MyInt
的值是正确的,在本例中为600,但是文本框
没有显示它
在我的输出
窗口中还出现以下错误:
System.Windows.Data Error: 6 : 'StringFormat' converter failed to convert value '600' (type 'Decimal'); fallback value will be used, if available. BindingExpression:Path=MyInt; DataItem='ViewModel' (HashCode=37975124); target element is 'TextBox' (Name='MyIntTextBox'); target property is 'Text' (type 'String') FormatException:'System.FormatException: Input string was not in a correct format.
at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter converter, Object value, Type targetType, Object parameter, CultureInfo culture)'
有什么简单的解决方法吗?还是我做错了什么
但是,当我在后面加上一些有效值时,就像这样
如果您在文本框
中键入十进制
值,它将再次工作
但解决方法是实现您自己的自定义转换器类,该类处理decimal
和string
之间的转换
试试这个:
namespace WpfApp2
{
class DecimalToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return null;
return System.Convert.ToDecimal(value).ToString(parameter as string);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
decimal d;
if (decimal.TryParse(value.ToString(), out d))
return d;
return value;
}
}
}
mm8的解决方案是好的,但在小数为空时失败。
下面是一个固定的ConvertBack函数:
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (decimal.TryParse(value.ToString(), out var d))
return d;
if (Nullable.GetUnderlyingType(targetType) != null)
return null;
else
return 0m;
}
如果(value==myInt){return;}没有帮助,请尝试删除。如果您在文本框中键入十进制值,它将再次工作。< /代码>它不会。不管怎样,你的解决方案非常有效。非常感谢。
<StackPanel xmlns:local="clr-namespace:WpfApp2">
<StackPanel.Resources>
<local:DecimalToStringConverter x:Key="conv" />
</StackPanel.Resources>
<TextBox x:Name="MyIntTextBox" Text="{Binding MyInt, Converter={StaticResource conv}, ConverterParameter=0.00}"/>
<Label Content="{Binding MyStr2}"/>
</StackPanel>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (decimal.TryParse(value.ToString(), out var d))
return d;
if (Nullable.GetUnderlyingType(targetType) != null)
return null;
else
return 0m;
}