1 year ago

#139282

test-img

veldo

How to override exception message generated by IValueConverter

I would like to override message provided by the custom conversion class which inherits IValueConverter interface.

public class BoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //string convertedValue = "";

            bool initialValue = (bool)value;

            if (initialValue == false)
            {
                return "0";
            }
            else if (initialValue == true)
            {
                return "1";
            }
            else return null;//{ return convertedValue; }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string initialValue = ((string)value).Trim();
            try {
                
                if (initialValue == "1") { return true; }
                else if (initialValue == "0") { return false; }
                else { return null; }
            } 
            catch (Exception) 
            {
                return null;
            }
            
        }
    }

For example, if I enter 3 into text box binded with property I am receiving message: Value '3' could not be converted. I would like to provide longer message.

c#

wpf

data-conversion

ivalueconverter

0 Answers

Your Answer

Accepted video resources