Page 1 of 1

Value Converter Group crashes build tool

Posted: 13 Mar 2015, 14:57
by markg
I am trying to integrate a value converter group so that I can invert a bool and then convert the result to a visibility. When I add the following to my xaml the build tool crashes Unity at startup:
<common:ValueConverterGroup x:Key="invertBoolAndChangeVisibilityConverter">
        <common:InvertBoolConverter/>
        <common:BoolToVisibilityConverter/>
      </common:ValueConverterGroup>
The code for the converter:
public class ValueConverterGroup : List<IValueConverter>, IValueConverter
	{
		public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			return this.Aggregate (value, (current, converter) => converter.Convert (current, targetType, parameter, culture));
		}
	
		public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException ();
		}
	}

Re: Value Converter Group crashes build tool

Posted: 16 Mar 2015, 21:37
by sfernandez
The crash comes from using a converter (IValueConverter) that also implements another interface (IList).
We don't support objects implementing multiple interfaces, but we should warn the user without crashing this way, we will fix this.

You can achieve the same result by using a property to contain children converters:
[System.Windows.Markup.ContentProperty("Children")]
public class ValueConverterGroup : IValueConverter
{
    public List<IValueConverter> Children { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        object current = value;
        foreach (var converter in Children)
        {
            current = converter.Convert(current, targetType, parameter, culture);
        }
        return current;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}