views:

349

answers:

3

i know it must something simple that i missed. I use data services to get data into my silverlight application. When i bind the data to my datagrid it works like a charm

LessonGrid.ItemsSource = context.Lessons

however as soon as i try to wrap my objects into more complex data structure it stops working

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

I tried to define binding with path and without and doesn't seem to work

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

Suggestions?


After more more research:

Ok,it's nothing to do with complex objects. Even this code shows two rows but no data. What am i missing?

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>
A: 

You have created a Linq Query but has not actually executed yet. In order to actually execute you must do something like .ToList() Try This:

 LessonGrid.ItemsSource = context.Lessons.Select(l => new {Lesson = l; Color=Colors.Yellow}).ToList();
Anthony
the query is executed when the DataGrid is rendered. I get as many rows in my grid as objects i have in the result, they are just all empty.
Vitalik
A: 

Are you sure that your Linq query return any items? And any items that include StartTime?

As I can see it your query returns an object that contain two parameters,Lesson and Color, but not StartTime. And I guess the separator between the parameters should be a comma (,).

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

Then you can bind to your property in your DataGrid:

<data:DataGridTextColumn Header="Date" Binding="{Binding StartTime}"/>
or
<data:DataGridTextColumn Header="Date" Binding="{Binding Path=StartTime}"/>
xamlgeek
my "Lesson" has property "DateTime StartTime", that's the field i am trying to bind to.
Vitalik
+1  A: 

Ok, i figured it out myself. It's something about implicit types that binding doesn't like. This shows empty grid

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

but this renders data.

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