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(); で更新しています。
すべてがうまく機能していますが、フォーカスが失われたときに convert が呼び出されず、テキストボックスに文字列を入力すると、convert が呼び出されず、textBox テキストが空白に変換されません。
誰でも私に提案してもらえますか、コードの問題は何ですか。
前もって感謝します。----ラージ