Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I've got a custom collection AllItems. Each item within this has string Make, string Model and List<stockitem> StockItems.

I have a datagrid bound to this custom collection showing the Make, Model and StockItems.Count. This works fine, but when the user clicks on one of the rows I have another datagrid to show the expanded details withing StockItems.

So effectively my 2nd datagrid needs to be bound to StockItems and also the Make and Model. I can bind two columns to StockItems, but I then wish to have the further details shown next to that data as follows:

datagrid 1 - ItemsSource is set to AllItems

HTML
<DataGrid x:Name="datagridStockMovements" AlternatingRowremoved="#FFE2E2E2" IsReadOnly="True" AlternationCount="2" AutoGenerateColumns="False" MouseDown="datagridStockMovements_MouseDown" Visibility="Hidden">
                                                <DataGrid.Columns>
                                                    <DataGridTextColumn Header="Manufacturer" MinWidth="200" Binding="{Binding Path=Manufacturer}" />
                                                    <DataGridTextColumn Header="Model" MinWidth="200" Binding="{Binding Path=ModelName}" />
                                                    <DataGridTemplateColumn Header="Quantity" MinWidth="200">
                                                        <DataGridTemplateColumn.CellTemplate>
                                                            <DataTemplate>
                                                                <DockPanel>
                                                                    <TextBlock Text="{Binding Path=ListOfDevices.Count}" />
                                                                </DockPanel>
                                                            </DataTemplate>
                                                        </DataGridTemplateColumn.CellTemplate>
                                                    </DataGridTemplateColumn >
                                                </DataGrid.Columns>
                                            </DataGrid>


datagrid2 - ItemsSource is set to StockItems and also needs to include AllItems.Model & AllItems.Manufacturer

HTML
<DataGrid x:Name="datagridStockMovementDetails" Grid.Row="1" IsReadOnly="True" AlternatingRowremoved="#FFE2E2E2" AlternationCount="2" AutoGenerateColumns="False" Visibility="Hidden">
                                    <DataGrid.Columns>
                                        <DataGridTextColumn Header="Serial" MinWidth="200" Binding="{Binding Path=Serial}" />
                                        <DataGridTextColumn Header="Machine Name" MinWidth="200" Binding="{Binding Path=MachineName}" />
                                        <DataGridTextColumn Header="Model" MinWidth="200" Binding="{Binding Path=Parent.Model}" />
                                        <DataGridTextColumn Header="Make" MinWidth="200" Binding="{Binding Path=Parent.Manufacturer}" />
                                    </DataGrid.Columns>
                                </DataGrid>


I'm struggling to explain this well I feel, but it seems that in the expanded datagrid I'm binding to the list within my custom object, then I also want to take the details of Make and Model into that binding with XAML.

Anyone know how to do this?

Regards,

Jib
Posted

1 solution

Ok, this is quite crude, but it gets you what you want:

XML
            <contentcontrol>
                Name="stockItems"
                DataContext="{Binding ElementName=datagridStockMovements, Path=SelectedValue}"> 

<DataGrid x:Name="datagridStockMovementDetails" Grid.Row="1" IsReadOnly="True" AlternatingRowremoved="#FFE2E2E2" AlternationCount="2" AutoGenerateColumns="False" Visibility="Hidden">
                                    <DataGrid.Columns>
                                        <DataGridTextColumn Header="Serial" MinWidth="200" Binding="{Binding Path=Serial}" />
                                        <DataGridTextColumn Header="Machine Name" MinWidth="200" Binding="{Binding RelativeSource={RelativeSource AncestorLevel=3, AncestorType={x:Type ContentControl}}, Path=DataContext.MachineName}"
                                        <DataGridTextColumn Header="Model" MinWidth="200" Binding="{Binding RelativeSource={RelativeSource AncestorLevel=3, AncestorType={x:Type ContentControl}}, Path=DataContext.Model}"
                                        <DataGridTextColumn Header="Make" MinWidth="200" Binding="{Binding Path=Parent.Manufacturer}" />
                                    </DataGrid.Columns>
                                </DataGrid>
</contentcontrol>
 
Share this answer
 
Comments
Jibrohni 24-Jan-12 4:55am    
That's great - it's working just as I'd hoped although it seems to replace the original datagrid with the second, rather than display both concurrently. Any ideas? They should display in different row of my grid but it doesn't seem to be working that way.
Jibrohni 24-Jan-12 5:13am    
Ignore that! Thank you very much for your help, that's great!
SteveAdey 24-Jan-12 6:02am    
No problem, glad to help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900