Mark
Topic Author
Posts: 4
Joined: 20 Apr 2021, 18:52

Compound ValueConverter in C++ ?

12 May 2021, 10:45

I'm trying to use a compound value converter, defined like this:
        <u:ValueConverterGroup x:Key="MyGroup1">
            <u:SomeValueConverter1/>
            <u:SomeValueConverter2/>
        </u:ValueConverterGroup>
For which the c# is this (from https://riptutorial.com/wpf/example/161 ... 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));
        }

        etc..
Though I'm not sure how to implement the ValueConverter in C++ to allow the child ValueConverters to be added. So far I've just implemented it as a standard ValueConverter. I think Noesis is expecting a method to add a child node, because I get this error in the output window:
"Can't assign child to parent"
I need to add the equivalent of this I guess: List<IValueConverter>

Can anyone help?

Thanks.
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Compound ValueConverter in C++ ?

12 May 2021, 14:06

Hi Mark,

You should be able to accomplish something like that by using a collection property in your converter. Then setting that property as the ContentProperty in the reflection.

C++
typedef Noesis::UICollection<Noesis::BaseValueConverter> ConverterCollection;

class ValueConverterGroup : public Noesis::BaseValueConverter
{
public:
    ValueConverterGroup(): _children(Noesis::MakePtr<ConverterCollection>()) { }
    ConverterCollection* GetChildren() const { return _children; }
    bool TryConvert(Noesis::BaseComponent* value, const Noesis::Type* targetType, Noesis::BaseComponent* parameter,
        Noesis::Ptr<Noesis::BaseComponent>& result) override
    {
        Noesis::Ptr<Noesis::BaseComponent> current(value);
        int numConverters = _children->Count();
        for (int i = 0; i < numConverters; ++i)
        {
            Noesis::BaseValueConverter* converter = _children->Get(i);
            if (!converter->TryConvert(current, targetType, parameter, current))
            {
                return false;
            }
        }
        result.Reset(current);
        return true;
    }

private:
    Noesis::Ptr<ConverterCollection> _children;
    NS_IMPLEMENT_INLINE_REFLECTION(ValueConverterGroup, Noesis::BaseValueConverter, "Test.ValueConverterGroup");
    {
        NsMeta<Noesis::ContentPropertyMetaData>("Children");
        NsProp("Children", &ValueConverterGroup::GetChildren);
    }
};
C#
[System.Windows.Markup.ContentProperty("Children")]
public class ValueConverterGroup : IValueConverter
{
    public ValueConverterGroup()
    {
        this.Children = new List<IValueConverter>();
    }
    public List<IValueConverter> Children { get; set; }
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        object current = value;
        if (Children.Count() > 0)
        {
            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();
    }
}
 
Mark
Topic Author
Posts: 4
Joined: 20 Apr 2021, 18:52

Re: Compound ValueConverter in C++ ?

12 May 2021, 15:16

Thank you! :-)

Who is online

Users browsing this forum: Ahrefs [Bot] and 86 guests