Welcome to MSDN Blogs Sign in | Join | Help

Lester's WPF blog


Simple, easy & beautiful

News

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)

    <Window.Resources>

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

    </Window.Resources>

    <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

 

Posted: Monday, November 02, 2009 3:30 PM by llester
Attachment(s): Dynamic Objects.zip

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker