Having clear button set value to null?

2 posts, 0 answers
  1. Rick Glos
    Rick Glos avatar
    70 posts
    Member since:
    Mar 2009

    Posted 20 Jul 2012 Link to this post

    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?
  2. Rick Glos
    Rick Glos avatar
    70 posts
    Member since:
    Mar 2009

    Posted 23 Jul 2012 Link to this post

    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;
        }
    }
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top