2009-11-18 87 views
0

我知道它必须简单,我错过了。我使用数据服务将数据导入到我的Silverlight应用程序中。当我将数据绑定到我的DataGrid它在Silverlight中绑定到一个复杂的对象

LessonGrid.ItemsSource = context.Lessons 

,但是只要我尝试换我物体进入更复杂的数据结构,它停止工作

LessonGrid.ItemsSource = context.Lessons.Select(l => new {Lesson = l; Color=Colors.Yellow}) 

我试图定义绑定的作品就像一个魅力与路径和没有,似乎并不工作

<data:DataGridTextColumn Header="Date" Binding="{Binding StartTime}"/> 
<data:DataGridTextColumn Header="Date" Binding="{Binding StartTime, Path=Lesson.StartTime}"/> 
<data:DataGridTextColumn Header="Date" Binding="{Binding Path=Lesson.StartTime}"/> 
<data:DataGridTextColumn Header="Date" Binding="{Binding StartTime, Path=Lesson}"/> 

建议?


更详细的研究后:

好吧,这是没有任何关系的复杂对象。即使这个代码显示两行但没有数据。我错过了什么?

LessonGrid.ItemsSource = 
new[] {new {Color = Colors.Yellow,StartTime = 12, Text="text"}, 
new {Color = Colors.Red, StartTime = 14, Text="text3"}}; 

XAML:

<data:DataGrid x:Name="LessonGrid" AutoGenerateColumns="True" Height="375" IsReadOnly="True"> </data:DataGrid> 
+0

为了澄清,StartTime是Lesson对象的属性。 – Vitalik 2009-11-18 12:05:07

回答

2

好吧,我理解了它自己。这是关于绑定不喜欢的隐式类型。这显示空网格

LessonGrid.ItemsSource = new[] {new {StartTime = 111, Text = "hi there"}}; 

但这呈现数据。

LessonGrid.ItemsSource = new[] {new Temp {StartTime = 111, Text = "hi there"}}; 
0

您已经创建了一个LINQ查询,但实际上并没有尚未执行。为了实际执行必须做类似.ToList() 试试这个:

LessonGrid.ItemsSource = context.Lessons.Select(l => new {Lesson = l; Color=Colors.Yellow}).ToList(); 
+0

查询在DataGrid呈现时执行。我在网格中获得与结果中的对象一样多的行,它们都是空的。 – Vitalik 2009-11-18 12:05:43

0

你确定你的Linq查询返回任何项目?还有任何包含StartTime的项目?

正如我所看到的,您的查询返回一个包含两个参数Lesson和Color但不包含StartTime的对象。我想参数之间的分隔符应该是一个逗号(,)。

LessonGrid.ItemsSource = context.Lessons.Select(l => new {Lesson=l, Color=Colors.Yellow, StartTime=12}); 

然后你就可以绑定到你的财产在你的DataGrid:

<data:DataGridTextColumn Header="Date" Binding="{Binding StartTime}"/> 
or 
<data:DataGridTextColumn Header="Date" Binding="{Binding Path=StartTime}"/> 
+0

我的“课程”具有属性“DateTime StartTime”,这是我试图绑定到的字段。 – Vitalik 2009-11-18 12:03:31