2009-11-18 85 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