在ItemsControl的DataTemplate中绑定DataGrid [英] Binding DataGrid Within A DataTemplate of ItemsControl

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

问题描述

我要设置的绑定发生了一个奇怪的问题。我有一个ItemsControl,用于渲染WrapPanel,其中包含用于其DataTemplate的DataGrid。最终,我想使用WrapPanel为绑定到ItemsControl的列表中的每个项目显示一个DataGrid。现在,它会创建正确数量的DataGrid和标头,但实际上没有数据绑定。我对WPF的经验不足,无法知道我会误入歧途。这些项本身是Tuple对象。为什么我的数据值没有绑定?

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 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>>();
        }
    }


推荐答案

您的GetDistinctCodeCounts为您提供 IEnumerable<元组<字符串,整数> >

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

但是您需要 IEnumerable< IEnumerable<元组<字符串,int>> >

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

Small test: Replace the property GetDistinctCodeCounts with this:

   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),
            }
           );

我的测试:

这篇关于在ItemsControl的DataTemplate中绑定DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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