views:

23

answers:

2

I found an example online that explains how to perform databinding to a ListBox control using LINQ in WPF. The example works fine but when I replicate the same code in Silverlight it doesn't work. Is there a fundamental difference between Silverlight and WPF that I'm not aware of?

Here is an Example of the XAML:

<ListBox x:Name="listBox1">
 <ListBox.ItemTemplate>
  <DataTemplate>
   <StackPanel>

     <TextBlock Text="{Binding Name}" FontSize="18"/>
     <TextBlock Text="{Binding Role}" />     

   </StackPanel>
  </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

Here is an example of my code behind:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
  string[] names = new string[] { "Captain Avatar", "Derek Wildstar", "Queen Starsha" };
  string[] roles = new string[] { "Hero", "Captain", "Queen of Iscandar" };

  listBox1.ItemSource = from n in names from r in roles select new { Name = n, Role = r}
}
+1  A: 

Silverlight does not support binding to anonymous types. (To be technically correct, Silverlight does not support reflecting against internal types, and since anonymous types are internal, this doesn't work). See this article for a simple workaround- you will merely need to create a model class to hold the data.

public class MyItem
{
   public string Name { get; set; }
   public string Role { get; set; }
}

listBox1.ItemSource = from n in names from r in roles select new MyItem() { Name = n, Role = r}
Charlie
Your answer fixed my databinding error perfectly. THANKS!
Petezah
+1  A: 

In Silverlight you can not bind to anonymous types. Silverlight requires the type of the item being bound to be public but anonymous types are by internal.

You will need to to create a public type to carry your results:-

public class MyItem
{
   public string Name {get; set; }
   public string Role {get; set; }
}

now in your code:-

listBox1.ItemSource = from n in names from r in roles select new MyItem() { Name = n, Role = r}    
AnthonyWJones
So... exactly what I said? There is an up-vote button which you can use instead of duplicating answers.
Charlie
@Charlie: Only I stated it with code. You'll note my answer only arrived 3 minutes after yours. I tend not to bother with jumping on the "new answers" message every time it pops up I just complete my own answer. Perhaps you should read through the FAQ. SO is not just about giving __an__ answer but trying to give __quality__ answers. The idea is to make SO the "goto" place to get answers quickly and clearly. I'll tell you what, improve the quality of your answer with a code example and let me knowm, then I'll up vote it. ;)
AnthonyWJones
Well if your code was anything revelatory and could not immediately be inferred by reading my answer, then sure, it might be worth it. But if it's simply restating a line of his code and adding a boilerplate class that I'm sure our friend here can manage, then I would consider it a duplicate answer.But now that I've added "teh codez" for easy copy-pasting, I suppose you can upvote my answer.
Charlie
@Charlie: I tried to be friendly and help you see some reason. When I posted my answer I saw yours but at the time mine IMO was better. Perhaps you should do some research on the "fastest gun in the west" problem that SO suffered with and to some degree still does. Your veiled accusation is in bad taste and I find it offensive. However I have, as I stated I would, upvoted your answer although now rather than willingly as I intended but begrudgingly.
AnthonyWJones