我正在使用 Silverlight 中的绑定。我已将 TextBox 与 Decimal 实体绑定。下面是绑定的代码片段。
<TextBox x:Name="AmountBox" Text="{Binding SelectedEntity.Amount,Mode=TwoWay,StringFormat=\{0:n2\},Converter={StaticResource DecimalBlankValueConverter}}" Validate="True" TextChanged="AmountBox_TextChanged" LostFocus="AmountBox_LostFocus"/>
下面是转换器代码。
decimal result;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!Decimal.TryParse(value.ToString(),out result) || (decimal)value == decimal.Zero)
return null;
return decimal.Parse(value.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || !Decimal.TryParse(value.ToString(), out result))
return 0.00;
return decimal.Parse(value.ToString());
}
在失去焦点时,我正在使用 GetBindingExpression(TextBox.TextProperty).UpdateSource();
一切都很好,但是在失去焦点时不会调用转换,当我在文本框中输入字符串时,不会调用转换,它不会将文本框文本转换为空白。
谁能建议我,代码中的问题是什么。
提前致谢。----拉杰