Binding an Entry to an Int32 property that allows negative numbers?

JonHuntJonHunt USMember ✭✭
edited May 2016 in Xamarin.Forms

I have a model with a property QtyIn which is an Int32. It's bound to an Entry in Xamarin Forms using this binding in XAML:

<Entry Text="{Binding Path=Source.QtyIn, Mode=TwoWay, Converter={StaticResource intToStringConverter}}" />

Without using my own IValueConverter, pressing the minus key on the keyboard simply does nothing (the binder has trouble converting '-' to an Int32). I get this error in my stdout:

05-03 15:19:27.923 I/mono-stdout(19384): Binding: - can not be converted to type 'System.Int32'

This is the code in my IValueConverter which tries to just return the string '-' if it can't be converted to a valid Int32.

    public class IntToStringConverter : IValueConverter
    {
        // from Int32 to String
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.ToString();
        }
        // String to Int
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int parsedInt = 0;
            if (int.TryParse(value.ToString(), out parsedInt))
            {
                return parsedInt;
            }
            return value;
        }
    }

The problem is that Xamarin Forms is missing the UpdateSourceTrigger property on the Binding, so I can't set this to LostFocus. Right now it's try to convert the Entry text to an integer character-for-character which obviously doesn't work because '-' cannot be converted to a valid integer.

Does anyone know how to bind an Int32 to an Entry Text which allows negative numbers?

Thanks.

Best Answers

Answers

  • HunumanHunuman GBMember ✭✭✭✭

    @JonHunt

    When parsing a negative number you need to use the other overload with the correct NumberStyles:

    TryParse( string s, NumberStyles style, IFormatProvider provider, out int result)

    For details please see:
    https://msdn.microsoft.com/en-us/library/zf50za27%28v=vs.110%29.aspx

    Hope this helps,

    Tim

  • JonHuntJonHunt USMember ✭✭

    I have done that using:

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                int parsedInt = 0;
                if (int.TryParse(value.ToString(), NumberStyles.AllowLeadingSign, null, out parsedInt))
                {
                    return parsedInt;
                }
    
                return value;
            }
    

    and it still removes the '-' character after I press '-'. There is no way to type -123 because the - can never be converted to an integer. I get the error:

    05-03 15:19:27.923 I/mono-stdout(19384): Binding: - can not be converted to type 'System.Int32'

  • HunumanHunuman GBMember ✭✭✭✭

    @JonHunt

    Hi Jon,

    That is strange. I use similar code within a framework under development, and it functions fine.
    Though that code is not in a ValueConvertor.

    What was the outcome when you used the CultureInfo as the format provider?

    Tim

  • JonHuntJonHunt USMember ✭✭

    I haven't tried using the CultureInfo but I'll see what that does. Basically it needs to convert only when you leave the focus but Xamarin Forms doesn't have that feature that WPF has

Sign In or Register to comment.