1 votes

C # IValueConverter Boolean to String

Hello friends I have a question I am learning to use IValueConverter and I need to convert a boolean to a string, I need that if my bool is true it returns "Carta en uso" and if it is false an empty string, this is what I have so far

 public class CardConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null)
        {
           // ConnCard es mi modelo y UsingCard es mi booleano
            var con = (ConnCard)value;
            return con.UsingCard; // no si debo regresar aqui el string que quiero
        }
        else
        {
            return false;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {           
        return value; // o aqui
    }
}

0voto

If someone serves you, my logic remains like this

   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null )
        {
            //var con = (MopDTO)value;
            bool val = (bool)value;
            if (val != false)
            {
                return "Carta en uso";
            }
            else
            {
                return " ";
            }
        }
        else
        {
            return " ";
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {           
        return value;
    }

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X