WPF ItemsControl并绑定到自身

问题描述 投票:0回答:1

我有一个项目控件绑定到类型'TypeA'的集合。此类型'TypeA'具有类型'TypeB'的集合。

在项控件数据模板中,我有一个数据网格,该数据网格为TypeB集合的每个项显示一行,并且为每一行显示一列变量'VariableOfTypeB'的值。

效果很好。

现在,我想在下面显示另一个数据网格,该数据网格将绑定到类型'TypeA'的集合的当前项目,并且将显示变量'VariableOfTypeA'的值。我似乎无法实现。

例如,我可以在数据网格外部访问'VariableOfTypeA',例如在标签中。

请给我小费吗?

<ItemsControl ItemsSource="{Binding TypeACollection}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
                <StackPanel>
                    <DataGrid ItemsSource="{Binding TypeBCollection}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Width="Auto"
                                                Binding="{Binding VariableOfTypeB />
                        </DataGrid.Columns>
                    </DataGrid>
                    <!-- cannot make this work -->
                    <DataGrid ItemsSource="{Binding}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Width="Auto"
                                                Binding="{Binding VariableOfTypeA />
                        </DataGrid.Columns>
                    </DataGrid>
                </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
wpf itemscontrol
1个回答
1
投票

ItemsControl没有要在某种意义上寻找的SelectedItem

您更适合使用DataGridListView,它具有选定的行属性(来自/来自您的A集合),然后可以将其绑定到子网格中以显示替代信息。

© www.soinside.com 2019 - 2024. All rights reserved.