如何使用不同的标题/列布局动态插入Silverlight 3数据网格中的行?

时间:2009-07-14 02:49:53

标签: silverlight

假设我拥有Silverlight 3中的DataGrid控件,并且我想在两行之间动态创建一些可用空间以显示更多详细信息。我怎样才能做到这一点?

Header1 | Header2 | Header3 | Header4
-------------------------------------
Cell1     Cell2     Cell3     Cell4
Cell5     Cell6     Cell7     Cell8
Cell9     Cell10    Cell11    Cell12
例如,

将成为:

Header1 | Header2 | Header3 | Header4
-------------------------------------
Cell1     Cell2     Cell3     Cell4
Cell5     Cell6     Cell7     Cell8
   Foo1    Foo2
   Foo3    Foo4
Cell9     Cell10    Cell11    Cell12

请注意,两个新插入的“行”可能具有不同的列数,可能是不同类型的控件。换句话说,插入的项目可能可能是另一个单一控件。

使用DataGrid控件是否可以实现?也许有人有一些聪明的主意。 非常感谢!

1 个答案:

答案 0 :(得分:0)

您将需要一个类型类(即Cell)的集合,其中包含在单元格上公开的属性.Foos集合。 (简短的示例属性)

Class Foo
   Property FooName as String
End Class

Class Cell
   Property CellName as String
   ReadOnly Property Foos as Generic.List(of Foo)
End Class

 <DataTemplate x:Key="MyTemplate">
   <TextBlock Text={Binding CellName}" />
   <StackPanel ItemSource="{Binding Foos}">
       <StackPanel.ItemTemplate>
           <DataTemplate>
               <TextBlock Text="{Binding FooName}" />
           <DataTemplate>
       <StackPanel.ItemTemplate>
   </StackPanel>
</DataTemplate>

对于那些单元格的DataGrid,您可以将CellTemplate设置为MyTemplate。对于那些具有空Foos系列的细胞,Foos将不会出现。