绑定List< KeyValuePair<字符串,List< DataTable>>.项目控制WPF [英] Binding a List<KeyValuePair<string, List<DataTable>>> items control wpf
问题描述
当列表中的键值对项展开时,我需要显示2个数据表.键值对项的键应该是扩展头. 第一个表应绑定到第一个数据网格,第二个表应绑定到第二个网格. 问题在于,ItemsControl上的第二个项目"显示与第一个相同的值. (我的猜测是数据网格的ItemsSource绑定不正确,因为VM正确地为ListOfKeyValuePairs生成了数据.)
I need to display 2 datatables when a key value pair item in the list is expanded. And the key value pair item's key should be the expander header. The first table should be bound to the first data grid and second to the second grid. The problem is that the second 'item' on the ItemsControl displays the same value as the first one. (My guess is that the datagrid's ItemsSource binding is incorrect since the VM generates the data for the ListOfKeyValuePairs correctly.)
<!-- Data grid template -->
<DataTemplate x:Key="ValuesTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="data grid header text" Margin="20,0,0,0" Grid.Row="2" />
<DataGrid Grid.Row="0" ItemsSource="{Binding [0]}" />
<DataGrid Grid.Row="1" ItemsSource="{Binding [1]}" />
</Grid>
</DataTemplate>
<!-- List of data tables -->
<ItemsControl ItemsSource="{Binding ListOfKeyValuePairs}" VirtualizingStackPanel.IsVirtualizing="True">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander IsExpanded="True" Margin="0,0,0,10">
<Expander.Header>
<TextBlock Text="{Binding Key}" Margin="0,0,20,0" VerticalAlignment="Top" />
</Expander.Header>
<ContentControl Content="{Binding Value}" ContentTemplate="{StaticResource ValuesTemplate}" ScrollViewer.CanContentScroll="True" Margin="20,0,0,0" />
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
推荐答案
您的代码运行正常.但是您在ValuesTemplate
中使用了Grid
.这两个项目
Your code is working perfectly. But you are using a Grid
inside your ValuesTemplate
. Both items
<DataGrid ItemsSource="{Binding [0]}" />
<DataGrid ItemsSource="{Binding [1]}" />
处于相同位置.一个DataGrid
覆盖了另一个DataGrid
.使用已定义的行,例如
are on the same position. One DataGrid
is covering the other DataGrid
. Use the defined rows like
<DataGrid Grid.Row="0" ItemsSource="{Binding [0]}" />
<DataGrid Grid.Row="1" ItemsSource="{Binding [1]}" />
或使用简单的StackPanel
.
这篇关于绑定List< KeyValuePair<字符串,List< DataTable>>.项目控制WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!