Jaap Taal

I¡¯m using an ExceptionValidationRule to display that a TextBox has an error.

The TextBox.Text property is bound to a property of type Nullable<decimal> (or decimal ).

I want the binding to write an unset value to the property when the TextBox contains an empty string.

WPF generates an error instead:

System.Windows.Data Error: 7 : ConvertBack cannot convert value '' (type 'String'). BindingExpressionStick out tongueath=¡­; DataItem='¡­' (HashCode=43994237); 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.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)

at System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)

at System.Decimal.Parse(String s, NumberStyles style, IFormatProvider provider)

at System.Convert.ToDecimal(String value, IFormatProvider provider)

at System.String.System.IConvertible.ToDecimal(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, ExceptionValidationRule exceptionValidationRule)'

I tried to use a custom NullableDecimalConverter, but when an exception is raised inside the converter, Visual Studio breaks and the exception is shown.

Please help!

FYI, this is the XAML I use, MyProperty is of type decimal :

<TextBox>

<TextBox.Text>

<Binding Path="MyProperty" UpdateSourceTrigger="PropertyChanged">

<!--Converter="{StaticResource NullableDecimalConverter}">-->

<Binding.ValidationRules>

<ExceptionValidationRule />

</Binding.ValidationRules>

</Binding>

</TextBox.Text>

</TextBox>



Re: Windows Presentation Foundation (WPF) Validation on Nullable<decimal>

Jaap Taal

bumpy





Re: Windows Presentation Foundation (WPF) Validation on Nullable<decimal>

Jaap Taal

Still looking for an answer!





Re: Windows Presentation Foundation (WPF) Validation on Nullable<decimal>

Jaap Taal

I was reading this interesting blog entry.

http://11011.net/archives/000695.html

I finally came up with a converter that does exactly what I want.

My solution is actually very simple. The builtin conversion mechanism does everything right but one thing: empty strings are passed to the Parse method. I want empty strings to result into null values:

class NullableConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
return string.Empty;
}
return value.ToString();
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// if the value is null, there is nothing to convert
// we want the empty string to be interpretted as null
if (value == null || string.IsNullOrEmpty(value.ToString()))
{
return null;
}
// Now let the builtin conversion handle the real conversion
return value;
}
#endregion
}