ICustomTypeProvider or DynamicObject

1.7k Views Asked by At

I'm looking to bind to a class that exposes its properties dynamically as per the code below. It seems that both ICustomTypeProvider and DynamicObject would work for this. Can anybody explain why I might want to use one over the other?

void CreateDynamicItem()
    {
        var di = new DynamicItem();
        di.AddProperty("Age", 16, typeof(int));
        di.AddProperty("Height", 5.2, typeof(double));
    }

<StackPanel>
    <Slider Value="{Binding Age}" />
    <Slider Value="{Binding Height}" />
</StackPanel>
1

There are 1 best solutions below

0
pastillman On

I found the answer here Binding to Dynamic Properties with ICustomTypeProvider under "What about WPF and DLR?"

The DLR objects such as ExpandoObject or DynamicObject (or any other implementation of the IDynamicMetaObjectProvider interface) do not carry any type information for their properties. The data binding engine, on the other hand, needs to get this information in order to perform type conversion for anything other than String.