Silverlight databinding error

Posted by Petezah on Stack Overflow See other posts from Stack Overflow or by Petezah
Published on 2010-05-26T22:43:18Z Indexed on 2010/05/26 23:01 UTC
Read the original article Hit count: 380

Filed under:
|
|
|
|

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}
}

© Stack Overflow or respective owner

Related posts about wpf

Related posts about LINQ