# Data binding in Silverlight 4

**URL:** <https://www.ruby-forum.com/t/data-binding-in-silverlight-4/195660>\
**Category:** IronRuby\
**Created:** [September 27, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/data-binding-in-silverlight-4/195660 "2010-09-27T20:26:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fxp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a5b964/32.png) [@fxp](https://www.ruby-forum.com/u/fxp)\
**Post date:** [September 27, 2010, 8:26pm UTC](https://www.ruby-forum.com/t/data-binding-in-silverlight-4/195660/1 "2010-09-27T20:26:21Z")

</div>

I’m beginning to research this, but I don’t want to reinvent the wheel  
if there is already a solution out there.

My understanding of the problem is this:  
Data binding in Silverlight does not work because the system doesn’t  
know how to handle dynamic objects. This is not the case in WPF because  
the binding system there uses type descriptors, but type descriptors  
aren’t available in Silverlight.

Is this your understanding as well?  
Has there been any creative solutions to this problem?  
Does anyone have details on what the binding system is really doing in  
Silverlight?

---

<div class="post-metadata">

**Author:** ![fxp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a5b964/32.png) [@fxp](https://www.ruby-forum.com/u/fxp)\
**Post date:** [September 27, 2010, 10:00pm UTC](https://www.ruby-forum.com/t/data-binding-in-silverlight-4/195660/2 "2010-09-27T22:00:44Z")

</div>

That’s my understanding. Silverlight will only reflect over properties,  
as  
it does not use type descriptors - nor does it know how to determine a  
dynamic object’s attributes.

If you use attr\_accessor, I believe that’ll result in corresponding  
properties being added to the generated class.

If all else fails, you could use the Silveright DataSet (  
[http://silverlightdataset.net/silverlightdataset/Default.aspx](http://silverlightdataset.net/silverlightdataset/Default.aspx)) project  
to  
create bindable objects. Behind the scenes, it will create a class with  
properties that match the column names you pass in. I used it in a  
recent  
project where I needed to display a DataGrid bound to the results of an  
IDataReader, where the columns weren’t known until runtime. Just  
throwin’ it  
out there.

-Charles

On Mon, Sep 27, 2010 at 1:26 PM, Christopher Bennage

---

<div class="post-metadata">

**Author:** ![fxp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a5b964/32.png) [@fxp](https://www.ruby-forum.com/u/fxp)\
**Post date:** [September 28, 2010, 3:54am UTC](https://www.ruby-forum.com/t/data-binding-in-silverlight-4/195660/3 "2010-09-28T03:54:53Z")

</div>

SIlverlight, unlike WPF, is incapable of binding to dynamic properties.  
Static properties, on the other hand are no problem. One way around  
this  
is to create a string indexer on your class (this[string index]) that  
returns the property by name. Then, in the XAML, you can bind using  
array  
(square-bracket) syntax: . I  
do  
this in C# a lot, and I am pretty sure it translates the same in  
IronRuby:

> **[HouseOfBliz | Blog Tentang Rumah, Desain Dan Umum](https://houseofbilz.com)**
>
> Dapatkan informasi dari HouseOfBliz Blog Tentang Rumah, Desain Dan Umum setiap hari, baik itu tren terkini dalam gaya hidup, mode, kecantikan, teknologi, keuangan, atau perjalanan.

[http://houseofbilz.com/archives/2010/05/14/adventures-in-mvvm-my-viewmodel-base-silverlight-support/](http://houseofbilz.com/archives/2010/05/14/adventures-in-mvvm-my-viewmodel-base-silverlight-support/)Good  
luck,  
Brian

On Mon, Sep 27, 2010 at 3:58 PM, Charles S. \<

---

<div class="post-metadata">

**Author:** ![fxp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a5b964/32.png) [@fxp](https://www.ruby-forum.com/u/fxp)\
**Post date:** [September 28, 2010, 11:31pm UTC](https://www.ruby-forum.com/t/data-binding-in-silverlight-4/195660/4 "2010-09-28T23:31:40Z")

</div>

Here’s some interesting background about what’s happened with  
IronPython:  
[http://devhawk.net/CategoryView,category,\_\_clrtype\_\_.aspx](http://devhawk.net/CategoryView,category, __clrtype__.aspx)  
I’m still reading through it all, but note in particular the post from  
April  
24, 2009:  
[http://devhawk.net/2009/04/20/Introducing+Clrtype+Metaclasses.aspx](http://devhawk.net/2009/04/20/Introducing+Clrtype+Metaclasses.aspx)

My guess is that concept of **clrtype** never made it over into  
IronRuby…

Brian, regarding your string indexer idea. I assume that you mean to  
implement the indexer on a C# class and then inherit from that in ruby?  
That  
idea has some merit, I’ll look into it.

---

<div class="post-metadata">

**Author:** ![fxp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a5b964/32.png) [@fxp](https://www.ruby-forum.com/u/fxp)\
**Post date:** [September 29, 2010, 8:47am UTC](https://www.ruby-forum.com/t/data-binding-in-silverlight-4/195660/5 "2010-09-29T08:47:08Z")

</div>

you can always check out my ironnails project that does it for wpf and  
uses  
dictionaries. [GitHub - casualjim/ironnails: An experiment for WPF with IronRuby as an MVC framework](http://github.com/casualjim/ironnails)

The databinding approach I used should work in silverlight too

On Tue, Sep 28, 2010 at 10:23 PM, Christopher Bennage \<

---

<div class="post-metadata">

**Author:** ![fxp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a5b964/32.png) [@fxp](https://www.ruby-forum.com/u/fxp)\
**Post date:** [September 28, 2010, 11:48pm UTC](https://www.ruby-forum.com/t/data-binding-in-silverlight-4/195660/6 "2010-09-28T23:48:39Z")

</div>

Christopher,

My example is in C# using dynamic, but there must be a way to write an  
Indexer in Ruby that can interop properly. If all else failed, you  
could  
write a C# decorator that exposes the Ruby properties dynamically  
through a  
string Indexer.

Good luck! Let us know what you figure out 🙂

B

On Sep 28, 2010 5:31 PM, “Christopher Bennage” \<  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\> wrote:

> Here’s some interesting background about what’s happened with IronPython:  
> [DevHawk – DevHawk](http://devhawk.net/CategoryView,category, __clrtype__.aspx)  
> I’m still reading through it all, but note in particular the post from  
> April  
> 24, 2009:  
> [DevHawk – DevHawk](http://devhawk.net/2009/04/20/Introducing+Clrtype+Metaclasses.aspx)
> 
> My guess is that concept of **clrtype** never made it over into IronRuby..
> 
> Brian, regarding your string indexer idea. I assume that you mean to  
> implement the indexer on a C# class and then inherit from that in ruby?  
> That

---

<div class="post-metadata">

**Author:** ![fxp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a5b964/32.png) [@fxp](https://www.ruby-forum.com/u/fxp)\
**Post date:** [October 8, 2010, 5:08am UTC](https://www.ruby-forum.com/t/data-binding-in-silverlight-4/195660/7 "2010-10-08T05:08:25Z")

</div>

Hi Ivan,

Could you expand a bit on your approach? For what I noticed it defines  
readers, writters and implements INPC. That’s ok for WPF, but I don’t  
see  
how that would help in Silverlight. I’m probably missing something.

Thanks  
Miguel
