New WPF Features: Binding to Dynamic Objects

This is part of a series on New WPF Features

C# 4.0 allows creation of dynamic objects which is useful when you are interacting with dynamic languages. If you are unfamiliar with the dynamic concept take a quick look at this blog entry

So back to WPF. We support binding to dynamic objects.. woohoo... To begin with you need to create the dynamic object (see attached proj)


        <local:DynamicObjectClass x:Key="MyDynamicObject" />


    <StackPanel Name="BindPanel" DataContext="{StaticResource MyDynamicObject}">

        <TextBox Text="{Binding Path=A}"/>

        <TextBlock Text="{Binding Path=B.C}" />

        <TextBox  Text="{Binding Path=[(x:Int32)0]}"/>


In the loaded event , we can then set values

            dynamic dynamicObj = BindPanel.DataContext ;

            dynamicObj.A = "Simple Binding";

            dynamicObj.B = new DynamicObjectClass();

            dynamicObj.B.C = "Nested Prop Binding";

            dynamicObj.AddItem("item 0");

            dynamicObj[0] = "Indexer Binding";


Project Code attached

Share this post