【发布时间】:2011-02-24 10:22:44
【问题描述】:
我在网上找到了一个示例,该示例解释了如何在 WPF 中使用 LINQ 对 ListBox 控件执行数据绑定。该示例工作正常,但是当我在 Silverlight 中复制相同的代码时,它不起作用。 Silverlight 和 WPF 之间是否存在我不知道的根本区别?
这是 XAML 的一个示例:
<ListBox x:Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" FontSize="18"/>
<TextBlock Text="{Binding Role}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
下面是我的代码示例:
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}
}
【问题讨论】:
标签: wpf linq silverlight data-binding listbox