This is a migrated thread and some comments may be shown as answers.

Having clear button set value to null?

1 Answer 196 Views
MaskedInput (Numeric, DateTime, Text, Currency)
This is a migrated thread and some comments may be shown as answers.
Rick Glos
Top achievements
Rank 1
Rick Glos asked on 20 Jul 2012, 02:31 PM
When I hit the clear button, it's sets the value to an empty string.  Can I have it set the value to null if I'm binding to a Nullable?

1 Answer, 1 is accepted

Sort by
0
Rick Glos
Top achievements
Rank 1
answered on 23 Jul 2012, 01:07 PM
I've accomplished it for now using a Converter.

<telerik:RadMaskedTextInput Grid.Row="7" Grid.Column="1" Value="{Binding Path=Shareholder.TaxIdSecondary, Mode=TwoWay, Converter={StaticResource RadMaskedTextInputStringToNullConverter}, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" IsEnabled="{Binding IsEditMode}" Margin="5" EmptyContent="Secondary Tax ID" Mask="###-##-####" TextMode="MaskedText" SelectionOnFocus="SelectAll" InputBehavior="Replace"></telerik:RadMaskedTextInput>


// This converter is to allow for setting the value to null when the user clicks the clear button on the RadMaskedTextInput
 
public class RadMaskedTextInputStringToNullConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // if value is a string and it's empty, return null
        if (value.GetType() == typeof(string))
        {
            var valueAsString = value as string;
            if (string.IsNullOrEmpty(valueAsString))
            {
                return null;
            }
        }
         
        // otherwise just return the value
        return value;
    }
}
Tags
MaskedInput (Numeric, DateTime, Text, Currency)
Asked by
Rick Glos
Top achievements
Rank 1
Answers by
Rick Glos
Top achievements
Rank 1
Share this question
or