Using Datagrid within RowDetailsTemplate of another Datagrid

ぃ、小莉子 提交于 2019-12-02 01:56:48

问题


I would like to use a DataGrid within the RowDetailsTempalte of another Datagrid. This inner Datagrid should have its columns bound to a property of the current object in the outer Datagrid. For example, if the outer Datagrid is displaying all contacts by first name and last name, if I select a row I should be able to see another Datagrid containing all phone numbers associated with that contact. What I am most interested in is how the data of the inner Datagrid binds to the data of the outer Datagrid. Here is some XAML that I have so far to start with:

<data:DataGrid MinHeight="700" x:Name="contacts">
                <data:DataGrid.Columns>                       
                    <data:DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"></data:DataGridTextColumn>
                    <data:DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"></data:DataGridTextColumn>                        
                 </data:DataGrid.Columns>
                <data:DataGrid.RowDetailsTemplate>
                    <DataTemplate>
                        <StackPanel Background="Black">
                            <StackPanel Background="White" Margin="16">
                                <data:DataGrid DataContext="Contact.Phones">

                                </data:DataGrid>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </data:DataGrid.RowDetailsTemplate>
            </data:DataGrid>

回答1:


The last answer on this thread helped me: How is access inner Datagrid in Silverlight?.

On the inner DataGrid I set ItemsSource="{Binding Phones}" and removed the DataContext.




回答2:


Use the RowDetailsTemplate instead: DataGrid.RowDetailsTemplate Property.

You can bind to the DetailsVisibilityChanged event, and that will pass you the DataContext of the row that was clicked. From there you can retrieve the details data and update the RowDetailsTemplate accordingly.



来源:https://stackoverflow.com/questions/989218/using-datagrid-within-rowdetailstemplate-of-another-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!