2009-11-18 3 views
0

내가 놓친 게 분명하다는 것을 알고 있습니다. 데이터 서비스를 사용하여 Silverlight 응용 프로그램에 데이터를 가져옵니다. 난 내 데이터 그리드에 데이터를 바인딩 할 때 그것은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 : 당신은 Linq에 쿼리를 작성했지만 실제로는 아직 실행되지 않은

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

명확히하기 위해 StartTime은 Lesson 객체의 속성입니다. – Vitalik

답변

2

좋아, 내가 나 자신을 알아 냈다. 바인딩이 좋아하지 않는 암시 적 타입에 관한 것입니다. 빈 그리드를 보여줍니다.

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

그러나 이것은 데이터를 렌더링합니다.

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

. 실제로 당신이 .ToList 같은 작업을 수행해야합니다 실행하기 위해() 이 시도 : 당신은 당신의 LINQ 쿼리는 어떤 항목을 반환해야

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

DataGrid가 렌더링 될 때 쿼리가 실행됩니다. 결과에있는 객체만큼 그리드에서 많은 행을 얻습니다. 모든 행은 비어 있습니다. – Vitalik

0

있습니까? StartTime이 포함 된 항목은 무엇입니까?

쿼리에서 볼 수 있듯이 검색어는 강의와 색상의 두 가지 매개 변수가 있지만 StartTime은 포함하지 않은 개체를 반환합니다. 그리고 매개 변수 사이의 구분 기호는 쉼표 (,) 여야합니다.

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

그런 다음 당신은 당신의 데이터 그리드에 속성에 바인딩 할 수 있습니다 :

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

내 "단원"에 "DateTime StartTime"속성이 있습니다.이 필드는 바인딩하려는 필드입니다. – Vitalik

관련 문제