绑定列表<键值对<字符串、列表<DataTable>>>项控件 wpf



当列表中的键值对项展开时,我需要显示2个数据表。键值对项的键应该是扩展头。第一个表应该绑定到第一个数据网格,第二个表应该绑定到第二个网格。问题是ItemsControl上的第二个"item"显示与第一个相同的值。(我的猜测是datagrid的ItemsSource绑定是不正确的,因为VM正确地生成ListOfKeyValuePairs的数据。)


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

您的代码运行良好。但是你用的是GridValuesTemplate里面。这两个项目

<DataGrid ItemsSource="{Binding [0]}" />
<DataGrid ItemsSource="{Binding [1]}" />

在同一位置。一个DataGrid覆盖着另一个DataGrid。使用像

这样的定义行
<DataGrid Grid.Row="0" ItemsSource="{Binding [0]}" />
<DataGrid Grid.Row="1" ItemsSource="{Binding [1]}" />

或者使用简单的StackPanel

最新更新