数据网格绑定问题 [英] Datagrid binding problem

查看:103
本文介绍了数据网格绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个自定义集合AllItems.其中的每个项目都有字符串Make,字符串Model和List< stockitem>. StockItems.

我有一个绑定到此自定义集合的数据网格,其中显示了Make,Model和StockItems.Count.这可以正常工作,但是当用户单击其中的一行时,我有另一个数据网格来显示带有StockItems的展开的详细信息.

因此,有效地将我的第二个数据网格绑定到StockItems以及Make和Model.我可以将两列绑定到StockItems,但是然后希望在该数据旁边显示更多详细信息,如下所示:

datagrid 1-ItemsSource设置为AllItems

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

<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设置为StockItems,并且还需要包含AllItems.Model& AllItems.Manufacturer



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

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



我正在努力地解释这种感觉,但是似乎在扩展的数据网格中,我绑定到自定义对象中的列表,然后我还想将Make和Model的详细信息纳入XAML的绑定中.

有人知道该怎么做吗?

问候,

Jib



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

推荐答案

好吧,这很粗糙,但是可以满足您的需求:

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

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


这篇关于数据网格绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆