0

私はそれが私が逃した単純な何かでなければならないことを知っています。データサービスを使用して、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}"/>

提案?


さらに調査した後:

わかりました、それは複雑なオブジェクトとは何の関係もありません。このコードでも2行が表示されますが、データは表示されません。私は何が欠けていますか?

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>
4

3 に答える 3

2

わかりました、私はそれを自分で理解しました。これは、バインディングが好まない暗黙の型に関するものです。これは空のグリッドを示しています

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

しかし、これはデータをレンダリングします。

LessonGrid.ItemsSource = new[] {new Temp {StartTime = 111, Text = "hi there"}};
于 2009-11-18T15:28:30.817 に答える
0

Linq クエリを作成しましたが、実際にはまだ実行していません。実際に実行するには、.ToList() のようなことをしなければなりません。これを試してください:

 LessonGrid.ItemsSource = context.Lessons.Select(l => new {Lesson = l; Color=Colors.Yellow}).ToList();
于 2009-11-18T05:35:57.853 に答える
0

Linq クエリがアイテムを返すことは確かですか? そして、StartTime を含む項目はありますか?

ご覧のとおり、クエリは、Lesson と Color の 2 つのパラメーターを含むオブジェクトを返しますが、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}"/>
于 2009-11-18T08:38:38.640 に答える