Compound ValueConverter in C++ ?
I'm trying to use a compound value converter, defined like this:
For which the c# is this (from https://riptutorial.com/wpf/example/161 ... converter-):
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 anyone help?
Thanks.
Code: Select all
<u:ValueConverterGroup x:Key="MyGroup1">
<u:SomeValueConverter1/>
<u:SomeValueConverter2/>
</u:ValueConverterGroup>
Code: Select all
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..
I need to add the equivalent of this I guess: List<IValueConverter>"Can't assign child to parent"
Can anyone help?
Thanks.
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: Compound ValueConverter in C++ ?
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++
C#
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++
Code: Select all
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);
}
};
Code: Select all
[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();
}
}
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 64 guests