How to pass an object in int to converter?

I give up how to do this?

class AmountIsTooHighConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //int amount = (int)value;
        //int amount = (int)(string)value;
        //int amount = (int)(value.ToString);
        //int amount = Int32.Parse(value);
        //int amount = (int)Convert.ChangeType(value, typeof(int));
        //int amount = Convert.ToInt32(value);
        if (amount >= 35)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}
+3
source share
4 answers

Both Convert.ToInt32, or Int32.Parsehave to work ... If they do not, then it's definitely not int;)
Try to set a breakpoint in the converter to see the value, it can show you why it's not working.

+4
source

If the value is actually a string object (with content representing an integer value), this gives the least overhead:

int amount = Int32.Parse((string)value);

Convert.ToInt32 , int, , , , int.

+2

Place the breakpoint in the opening bracket of your Convert method. Run the application in debug mode. When a breakpoint hits, analyze the actual type of value. Take a throw. If the value is a string, then pass it to the string and then parse the result to get an integer. Run the program again. It should work this time.

0
source

If the value is an object, ToString()it is best to convert to a string before parsing toInt int amount = Int32.Parse(value.ToString());

0
source

Source: https://habr.com/ru/post/1709186/


All Articles