Question

I have a ContentProperty set on one of my class but when i coded in XAML it produces a XAMLParseException. Was wondering if anyone can advice me on what i can do.

Code Behind:

[ContentProperty(Name="Converters")]
class ChainedValueConverter: DependencyObject, IValueConverter
{
    public static readonly DependencyProperty ConvertersProperty = DependencyProperty.Register("Converters", typeof(ObservableCollection<IValueConverter>), typeof(ChainedValueConverter), null);

    public ChainedValueConverter()
    {
        SetValue(ConvertersProperty, new ObservableCollection<IValueConverter>());
    }

    public ObservableCollection<IValueConverter> Converters
    {
        get
        {
            return (ObservableCollection<IValueConverter>)GetValue(ConvertersProperty);
        }
    }

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        object _cValue = value;
        foreach (IValueConverter converter in Converters)
        {
            _cValue = converter.Convert(_cValue, targetType, parameter, language);
            if (_cValue == DependencyProperty.UnsetValue)
                break;
        }
        return _cValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        object _cValue = value;
        foreach (IValueConverter converter in Converters)
        {
            _cValue = converter.ConvertBack(_cValue, targetType, parameter, language);
            if (_cValue == DependencyProperty.UnsetValue)
                break;
        }
        return _cValue;
    }
}
/// <summary>
/// Converts boolean value to visibility.
/// </summary>
class BooleanToVisibilityConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return (Boolean)value ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return (Visibility)value == Visibility.Visible ? true : false;
    }
}

XAML:

<common:ChainedValueConverter x:Key="A">
            <common:BooleanToVisibilityConverter />
    </common:ChainedValueConverter>

The above codes when ran produces a XamlParseException with WinRT information: E_UNKNOWN_ERROR

Was it helpful?

Solution

I really like your idea of a chained converter and tried to find a solution. It seems you simply need to initialize your collection in the constructor (create a new instance). See my implementation:

[ContentProperty(Name = "Converters")]
public class ChainedConverter : DependencyObject, IValueConverter
{
    public ChainedConverter()
    {
        Converters = new ObservableCollection<IValueConverter>();
    }

    public static readonly DependencyProperty ConvertersProperty =
        DependencyProperty.Register("Converters", typeof (ObservableCollection<IValueConverter>), 
        typeof (ChainedConverter), new PropertyMetadata(default(ObservableCollection<IValueConverter>)));

    public ObservableCollection<IValueConverter> Converters
    {
        get { return (ObservableCollection<IValueConverter>) GetValue(ConvertersProperty); }
        set { SetValue(ConvertersProperty, value); }
    }

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        foreach (var c in Converters)
            value = c.Convert(value, targetType, parameter, language);
        return value; 
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        foreach (var c in Converters)
            value = c.ConvertBack(value, targetType, parameter, language);
        return value; 
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top