在ItemsControl的DataTemplate中绑定DataGrid

时间:2023-01-19 09:13:16

I have a strange issue occurring with the binding I'm trying to set up. I have an ItemsControl which I'm using to render a WrapPanel which contains a DataGrid for its DataTemplate. Ultimately I want to use the WrapPanel to show one DataGrid for each item in the list that gets bound to the ItemsControl. Right now it creates the correct number of DataGrids and headers, but there is no data actually being bound. I'm not experienced enough with WPF to know where I'm going astray here. The items themselves are Tuple objects. Why aren't my data values getting bound?

我正在尝试设置绑定时出现一个奇怪的问题。我有一个ItemsControl,我用它来渲染一个WrapPanel,其中包含DataTemplate的DataGrid。最后,我想使用WrapPanel为列表中绑定到ItemsControl的每个项目显示一个DataGrid。现在它创建了正确数量的DataGrids和标头,但实际上没有绑定数据。我对WPF的经验不足以知道我在哪里误入歧途。这些项本身就是Tuple对象。为什么我的数据值不受约束?

<ItemsControl ItemsSource="{Binding Path=GetDestinctCodeCounts}" Grid.Row="2" Grid.ColumnSpan="6" HorizontalAlignment="Center" HorizontalContentAlignment="Stretch">
  <ItemsControl.Template>
    <ControlTemplate>
      <WrapPanel IsItemsHost="True" />
    </ControlTemplate>
  </ItemsControl.Template>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <DataGrid AutoGenerateColumns="False" IsReadOnly="True" Margin="2,0,2,2" ItemsSource="{Binding}">
        <DataGrid.Columns>
          <DataGridTextColumn Width="Auto" Binding="{Binding Item1}" Header="Code" />
          <DataGridTextColumn Width="Auto" Binding="{Binding Item2}" Header="Count" />
        </DataGrid.Columns>
      </DataGrid>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

public List<Tuple<string, int>> GetDestinctCodeCounts
    {
        get
        {
            if (UnQualifiedZips.Count > 0)
            {
                var distinctCount = UnQualifiedZips.GroupBy(x => x.Item2).Select(x => new Tuple<string, int>(x.Key, x.Count())).ToList();
                return distinctCount;
            }
            else return new System.Collections.Generic.List<Tuple<string, int>>();
        }
    }

1 个解决方案

#1


0  

Your GetDistinctCodeCounts gives you an IEnumerable < tuple < string, int > >.

您的GetDistinctCodeCounts为您提供了IEnumerable >。

But you need an IEnumerable< IEnumerable< tuple < string,int>> >

但是你需要一个IEnumerable >

Small test: Replace the property GetDistinctCodeCounts with this:

小测试:将属性GetDistinctCodeCounts替换为:

   List<List<Tuple<string, int>>> tuples = new List<List<Tuple<string, int>>>();
        tuples.Add(
            new List<Tuple<string, int>>()
            {
             new Tuple<string,int>("a",1),
             new Tuple<string,int>("b",2),
            }
            );

        tuples.Add(
           new List<Tuple<string, int>>()
            {
             new Tuple<string,int>("a",1),
             new Tuple<string,int>("b",2),
            }
           );

My test:

我的测试:

在ItemsControl的DataTemplate中绑定DataGrid

#1


0  

Your GetDistinctCodeCounts gives you an IEnumerable < tuple < string, int > >.

您的GetDistinctCodeCounts为您提供了IEnumerable >。

But you need an IEnumerable< IEnumerable< tuple < string,int>> >

但是你需要一个IEnumerable >

Small test: Replace the property GetDistinctCodeCounts with this:

小测试:将属性GetDistinctCodeCounts替换为:

   List<List<Tuple<string, int>>> tuples = new List<List<Tuple<string, int>>>();
        tuples.Add(
            new List<Tuple<string, int>>()
            {
             new Tuple<string,int>("a",1),
             new Tuple<string,int>("b",2),
            }
            );

        tuples.Add(
           new List<Tuple<string, int>>()
            {
             new Tuple<string,int>("a",1),
             new Tuple<string,int>("b",2),
            }
           );

My test:

我的测试:

在ItemsControl的DataTemplate中绑定DataGrid