views:

3125

answers:

2

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

Use the rowdetailstemplate instead

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.rowdetailstemplate(VS.95).aspx

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.

Jason Watts
I cant find any reference to that event. Is there a better link that refers that property in detail? Also I am already using the RowDetailsTemplate.
Corey Sunwold
A: 

The last answer on this thread helped me: http://silverlight.net/forums/t/16838.aspx

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

Corey Sunwold