views:

379

answers:

2

Start a new Silverlight application... and in the code behind (in the "Loaded" event), put this code:

// This will *NOT* cause an error.
this.LayoutRoot.DataContext = new string[5];

But...

// This *WILL* cause an error!
this.LayoutRoot.DataContext = this;

The error that is raised is "Value does not fall within the expected range." BTW, this code works 100% in regular WPF (Windows development), and there should be no reason why it won't work in WPF/E :)

Any thoughts?

+4  A: 

You can't currently use visual elements as a data source for data binding in Silverlight 2. I think this is slated to be added for Silverlight v.Next.

Bill Reiss
Should have known ol' bill would have the answer :) So I'm guessing I'll have to make some pointless intermediate object to store my properties on and then use that as the DataContext.
Timothy Khouri
A: 

You can use visual elements as data source if you create binding directly in the code, but trying to assign visual element to DataContext will throw ArgumentException. It doesn't make much sense, but silverlight is just on version 2.