2011-03-21 73 views
0

我有一个绑定到自定义类的ObservableCollection的DataGrid。该集合是ViewModel中的一个属性,网格工作正常,包括对集合进行更改。在WPF中,如何在DataGrid.RowDetailsTemplate中绑定?

我想使用RowDetailsTemplate在StackPannel显示两个附加的集合,例如:

 <DataGrid.RowDetailsTemplate> 
      <DataTemplate> 
       <StackPanel Name="GrcidSP" Orientation="Horizontal"> 
        <DataGrid Name="ClusterIndexGrid" AutoGenerateColumns="True" 
           ItemsSource="{Binding ClusterIndexColumns}" 
           CanUserAddRows="False" CanUserResizeRows="False" 
           Width="Auto"/> 
        <DataGrid Name="IndexGrid" AutoGenerateColumns="True" 
           ItemsSource="{Binding IndexColumns}" 
           CanUserAddRows="False" CanUserResizeRows="False" 
           Width="Auto"/> 
       </StackPanel> 
      </DataTemplate> 
     </DataGrid.RowDetailsTemplate> 

ClusterIndexColumns和IndexColumns,内格栅的结合来源,也可在视图模型集合。

问题是,当我显示rowdetails,datagrids是空的,即根本没有加载,没有列或行。

在出价明白是怎么回事,我更换了内部数据网格与标签:

 <DataGrid.RowDetailsTemplate> 
      <DataTemplate> 
       <StackPanel Name="GridSP" Orientation="Horizontal"> 
        <Label Content="{Binding}" Width="Auto" Background="AliceBlue"/> 
       </StackPanel> 
      </DataTemplate> 
     </DataGrid.RowDetailsTemplate> 

,并没有指定绑定源 - {结合}

的标签现在显示的名字属于外部网格源的集合中的自定义类的位置。

所以不知何故,我需要回到ViewModel作为内部网格的数据上下文。

但是,如何祈祷呢?

回答

0

好的,所以我通过将ClusterIndexColumns和IndexColumns集合作为属性添加到主网格中显示的自定义类来解决此问题。现在,当我点击该行时,rowdetails区域打开,内部网格加载正常。

BTW,当所选行更改主电网,由selectedIndex属性绑定到视图模型的属性,使用

Mode=OneWayToSource 

希望这可以帮助别人的集合填充。