3

在我的程序中,我目前有一个带有修改后的行模板的数据网格,它显示一个列表视图。因此,当用户在数据网格中选择一行时,该行会展开并显示一个列表视图,而当用户选择不同的行时,原始列表会折叠,新行的列表会展开。我现在被要求让它允许扩展多个列表视图,但我不确定如何做到这一点。

我的xml代码:

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

因此,我的数据网格中显示了一个帐户列表,并且在每个帐户上都有一个详细信息列表,该列表显示在选择帐户时出现的列表视图中。How can i keep the details from the originally selected account in the listview visible when other accounts are selected?

4

1 回答 1

0

如果您希望先前选定行的详细信息模板保持可见,您可以这样尝试。您订阅 DataGrid 的RowDetailsVisibilityChanged事件,例如

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

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

我希望这将有所帮助。

于 2013-08-20T15:02:05.820 回答