父子Datagreid在MVVM中具有不同的ItemsSource

时间:2013-09-14 20:02:29

标签: wpf silverlight xaml mvvm mvvm-light

我有一个DataGrid,在DataGrid的DataGrid.RowDetailsTemplate中我放了另一个DataGrid.Now我的第一个Datagrid有ItemsSource =“{Binding MatchObsCollection}”和我的DataGrid.RowDetailsTemplate中的我的内部Datagrid有单独的ItemSource。但内部Datagrid不是人口。我正在使用MVVM,我在Viewmodel上通过ObservableCollections绑定了Datagrid。任何人都可以帮助我(我的问题几乎与Displaying hierarchal parent child data in WPF DataGrid相同,但我也没有找到任何好的答案)。我的xaml代码附在下面。感谢你,

<DataGrid HorizontalAlignment="Stretch" Margin="0" x:Name="dataGridParent" ItemsSource="{Binding MatchObsCollection}"
                      Style="{StaticResource EfesDataGridStyle}" FrozenColumnCount="14" GridLinesVisibility="All" 
                      HorizontalGridLinesBrush="#DADADA" VerticalGridLinesBrush="#DADADA" Cursor="Hand" AutoGenerateColumns="False" 
                      RowDetailsVisibilityMode="Visible" 
                      util:ParentMatchDataGridRowClickHandler.MethodName="ParentMatchDatagrdRowClick"
                      SelectedItem="{Binding SelectedMatch}"
                      >
<DataGrid.Columns>
                    <DataGridTextColumn Header="Kod" Binding="{Binding MatchNo}"  Width="50*" />
                    <DataGridTextColumn Header="Tarih" Binding="{Binding MatchDate}" Width="70*"/>
                    <DataGridTextColumn Header="Ev Sahibi" Binding="{Binding MatchHome}" Width="100*"/>
                    <DataGridTextColumn Header="Konuk" Binding="{Binding MatchVisitor}" Width="50*"/>
                    <DataGridTextColumn Header="1" Binding="{Binding MatchResultHomeOdds}" Width="30*" ></DataGridTextColumn>                 

                    <DataGridTextColumn Header="X" Binding="{Binding MatchResultDrawOdds}" Width="30*" />
                    <DataGridTextColumn Header="2" Binding="{Binding MatchResultVisitorOdds}" Width="30*"/>
                    <DataGridTextColumn Header="T1" Binding="{Binding MatchresultSingleHomeOdds}" Width="30*" />
                    <DataGridTextColumn Header="TX" Binding="{Binding MatchresultSingleDrawOdds}" Width="30*" />
                    <DataGridTextColumn Header="T2" Binding="{Binding MatchresultSingleVisitorOdds}" Width="30*" />
                    <DataGridTextColumn Header="+"  CellStyle ="{StaticResource DataGridCellStyle}"  Binding="{Binding TotalBets}" Width="30*"/>


                    <DataGridTextColumn Header="Min" Binding="{Binding MatchMin}" Width="30*" />
                    <DataGridTextColumn Header="Lig" Binding="{Binding TeamName}" Width="70*" />

                    <DataGridTemplateColumn Header="Durum" Width="Auto" >
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ToggleButton  x:Name="toggleBtn" VerticalAlignment="Center" Margin="0" Style="{StaticResource SlidingToggleButton}" BorderBrush="{x:Null}" IsChecked="{Binding ToggleBtn}" Background="#FF2F63FF" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>                    
                </DataGrid.Columns>

<DataGrid.RowDetailsTemplate x:Uid="test123">
                    <DataTemplate x:Name="testTemplete">
                        <DataGrid x:Name="innerGrid" ItemsSource="{Binding SubBetMatchDataListforChildGrid}" 
                                  Style="{StaticResource EfesDataGridStyle}" Margin="50,2,2,2" MinHeight="40"  
                                  GridLinesVisibility="All" HorizontalGridLinesBrush="#DADADA" VerticalGridLinesBrush="#DADADA" 
                                  Visibility="Collapsed" >

<DataGrid.Columns>
                                <DataGridTextColumn Header="Kod" Binding="{Binding Code}" Width="3*" />
                                <DataGridTextColumn Header="Alt Secenakler" Binding="{Binding SubOptions}" Width="7*" />                                
                            </DataGrid.Columns>

</DataGrid>                      
                    </DataTemplate>
                </DataGrid.RowDetailsTemplate>
            </DataGrid>

1 个答案:

答案 0 :(得分:3)

  

我正在使用MVVM,我正在绑定Datagrid   Viewmodel上的ObservableCollections。

您的ViewModel中应该只有一个ObservableCollectionMatchObsCollection)。 如果您在ViewModel中有MatchObsCollectionSubBetMatchDataListforChildGrid,则此代码无效。

DataContext的{​​{1}}是行项目。

因此,如果您想要绑定到ViewModel中的RowDetails,您应该在ObservableCollection中使用以下代码:

RowDetailsTemplate

如果要绑定到选定的对象内部... <DataGrid.RowDetailsTemplate x:Uid="test123"> <DataTemplate x:Name="testTemplete"> <DataGrid x:Name="innerGrid" ItemsSource="{Binding Path=DataContext.SubBetMatchDataListforChildGrid, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" ... ,您的类应如下所示:

ObservableCollection

在ViewModel中你应该有class Foo { public int MatchNo { get; set; } public DateTime MatchDate { get; set; } // more properties public ObservableCollection<Boo> SubBetMatchDataListforChildGrid { get; set; } } class Boo { public string Code { get; set; } public string SubOptions { get; set; } } Foo:

ObservableCollection