1

我有一个 DataGrid,在那个 DataGrid 的 DataGrid.RowDetailsTemplate 中我放了另一个 DataGrid。现在我的第一个 Datagrid 有 ItemsSource="{Binding MatchObsCollection}" 并且我的 DataGrid.RowDetailsTemplate 中的内部 Datagrid 有单独的 ItemSource。但是内部 Datagrid 没有填充。我正在使用 MVVM,并且我通过 Viewmodel 上的 ObservableCollections 绑定了 Datagrid 。谁能帮助我(我的问题与在 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>
4

1 回答 1

4

我正在使用 MVVM,并且我通过 Viewmodel 上的 ObservableCollections 绑定了 Datagrid 。

您的 ViewModel 中应该只有一个ObservableCollection( MatchObsCollection)。如果您有MatchObsCollection并且SubBetMatchDataListforChildGrid在 ViewModel 中,则此代码不起作用。

DataContextRowDetails行项目。

因此,如果您想绑定到ObservableCollectionViewModel 中的,您应该使用以下代码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你的类应该如下所示:

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; }
}

在 ViewModel 中你应该有ObservableCollectionFoo:

class MainViewModel
{
    public ObservableCollection<Foo> MatchObsCollection { get; set; }   
}
于 2013-09-14T20:33:41.283 回答