如何在数据网格中显示多个列表视图

时间:2013-08-20 13:59:57

标签: wpf xaml listview datagrid

在我的程序中,我目前有一个带有修改过的行模板的数据网格,它显示了一个列表视图。因此,当用户在数据网格中选择一行时,该行会展开并显示一个列表视图,当用户选择另一行时,原始列表会折叠,新行的列表会展开。我现在被要求允许扩展多个列表视图,并且不确定如何执行此操作。

我的xaml代码:

<DataGrid ItemsSource="{Binding Path=Accounts}" SelectedItem="{Binding Path=SelectedAccount}" AutoGenerateColumns="False"
              CanUserAddRows="False" CanUserDeleteRows="False" Width="Auto">
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <ListView MaxHeight="200" ItemsSource="{Binding Path=DataContext.SelectedAccount.Details, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
                          SelectedItem="{Binding Path=DataContext.SelectedDetail, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" 
                          Tools:GridViewSort.AutoSort="True" >
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Account" DisplayMemberBinding="{Binding Path=AccountCode}" 
                                            Tools:GridViewSort.PropertyName="AccountCode" Width="Auto" />

                            <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Path=Description}"
                                            Tools:GridViewSort.PropertyName="Description" Width="Auto"/>                                  
                        </GridView>
                    </ListView.View>
                    <ListView.Style>
                        <Style TargetType="ListView">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=DataContext.SelectedAccount.Details.Count, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" Value="0">
                                    <Setter Property="Visibility" Value="Collapsed" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ListView.Style>
                </ListView>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Account" Binding="{Binding Path=AccountCode}"/>
            <DataGridTextColumn Header="Title" Binding="{Binding Path=AccountTitle}" />
        </DataGrid.Columns>
    </DataGrid>

所以我有一个在我的数据网格中显示的帐户列表,每个帐户上都有一个详细列表,列表视图显示在选择帐户时显示。如果选择其他帐户,如何保持列表视图中最初选定帐户的详细信息?

1 个答案:

答案 0 :(得分:0)

如果您希望以前选定的行的detailsTemplate保持可见,您可以尝试这样做。您订阅了DataGrid的 RowDetailsVisibilityChanged 事件,如

<DataGrid  RowDetailsVisibilityChanged="DataGrid_RowDetailsVisibilityChanged_1">
        .......
        ........
    </DataGrid>

private void DataGrid_RowDetailsVisibilityChanged_1(object sender, DataGridRowDetailsEventArgs e)
    { 
        e.Row.DetailsVisibility= Visibility.Visible;
    }

我希望这会有所帮助。