ExpandoObject in Silverlight 4

I do not think, this is a recurring Silverlight pattern. Once in a while, we run into a situation where we need to work with dynamic classes. One best example is creating pivot using Silverlight. Well, by default when I say pivot, it is supposed to be read only control. Component One has a very powerful pivot control for Silverlight called OLAP for Silverlight. Lets say, you need work on the pivot data but you need to edit it, even then, you do not need to create a run time class to solve the problem. You can get away without using run time classes. But for the fun of it, I thought I will see if we can solve the problem by creating the dynamic class.

As always I develop Silverlight MVVM application using Jounce. So is the case here as well. So what are we trying to do? Before we go into creating pivot class lets first look at a simple solution using dynamic. We are going to display a grid of text block to show first name, last name, age and salary like the following

image

Lets start with our simple XAML

Code Snippet
  1. <TextBlock Text="First Name" FontSize="14"/>
  2.         <TextBlock Text="{Binding FirstName}" FontSize="14" Grid.Column="1"/>
  3.         <TextBlock Text="Last Name" FontSize="14" Grid.Row="1"/>
  4.         <TextBlock Text="{Binding LastName}" FontSize="14" Grid.Row="1" Grid.Column="1"/>
  5.         <TextBlock Text="Age" FontSize="14" Grid.Row="2"/>
  6.         <TextBlock Text="{Binding Age}" FontSize="14" Grid.Row="2" Grid.Column="1"/>
  7.         <TextBlock Text="Salary" FontSize="14" Grid.Row="3"/>
  8.         <TextBlock Text="{Binding Salary}" FontSize="14" Grid.Row="3" Grid.Column="1"/>

In the XAML we are doing direct binding to the view model properties. Lets look at the View Model code for these properties

Code Snippet
  1.  
  2. public string FirstName
  3. {
  4.     get
  5.     {
  6.         return person.FirstName;
  7.     }
  8. }
  9.  
  10. public string LastName
  11. {
  12.     get
  13.     {
  14.         return person.LastName;
  15.     }
  16. }
  17.  
  18. public int Age
  19. {
  20.     get
  21.     {
  22.         return person.Age;
  23.     }
  24. }
  25.  
  26. public int Salary
  27. {
  28.     get
  29.     {
  30.         return person.Salary;
  31.     }
  32. }

This code basically returns each property of person instance of a class. One would think it is a our favorite ‘Person’ class but that is the not the case. person is defined as

Code Snippet
  1. dynamic person = new ExpandoObject();

look at that we did not define the Person class rather we declared as a dynamic of type ExpandoObject. The beauty of this type is that, during run time you can add and remove members. Now the person is defined, how do we add properties to the class. Here is how, you can make members on the fly.

Code Snippet
  1.  
  2. public MainViewModel()
  3. {
  4.     person.FirstName = "unni";
  5.     person.LastName = "nair";
  6.     person.Age = 20;
  7.     person.Salary = 10000;
  8. }

Please make sure you add Microsoft.CSharp.dll otherwise you will get  a compile time error.

That is it, compile and run it, you will the result as shown in the beginning of the blog.

Hopefully you read through the MSDN reference on ExpandoObject. If you read all the way towards end, there is one section that is of very much interest to us. ‘Receiving Notifications of Property Changes’. As per MSDN, “ExpandObject class implements the INotifyPropertyChanged interface and can raise a PropertyChanged event when a member is added, deleted, or modified.” That is a very good news for us. But please don’t get carried away, there is a catch, it direct dynamic binding only works in WPF not in Silverlight. We will look at the issue in the next blog and see how we can solve it.

Advertisements

One thought on “ExpandoObject in Silverlight 4

  1. Pingback: ExpandoObject in Silverlight – Take 2 « Unni's space

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s