一个DataGridColumn动态生成的数据绑定 [英] DataGridColumn Binding with Dynamically Generated Data

查看:114
本文介绍了一个DataGridColumn动态生成的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大集,是从我的程序的web服务生成的数据。我需要在DataGrid中显示此数据,但数据的收集回来都有不同的格式(其中一个可能是3列,并在接下来的7),所以我不能创建一个特定的对象来保存这些数据。我无法得到这个信息显示。现在我想用这个方法。我把数据转换成KeyValuePairs的二维表。关键是这个数据将属于,值是该行的输出列。这就是我想现在。

I have a large set of data that is generated from a web service in my program. I need to display this data in a DataGrid, but the returned collections of data all have different formats (One may be 3 columns and the next 7) so I cannot create a specific object to hold this data. I am having trouble getting this information to display. Right now I am trying to use this method. I put the data into a two-dimensional list of KeyValuePairs. The Key being the Column this data would belong to, the Value being the output for that row. This is what I am trying right now.

private void SetDataValuesExecute(List<List<KeyValuePair<string,object>>> Data)
    {
        ResultsCollection = Data;
        ValuesDataGrid.Columns.Clear();
        foreach (string colName in Data[0].Select(x => x.Key).ToList())
        {
            DataGridTextColumn tempCol = new DataGridTextColumn();
            tempCol.Header = colName;
            Binding bind = new Binding();
            bind.Source = ResultsCollection.Select(x => x.FirstOrDefault(y => y.Key == colName).Value.ToString()).ToList();
            tempCol.Binding = bind;
            ValuesDataGrid.Columns.Add(tempCol);         
        }
    }

的ResultsCollection是用作我的绑定的源极的非易失性变量。这是所有二维表的数据我需要构建DataGrid的副本。

The ResultsCollection is a non-volatile variable to be used as the source of my bindings. It is a copy of all of the two-dimensional List data I need to construct the DataGrid.

这看起来在第一项中的提取列标头值,并创建基于该新列。结合语句看起来在每一行,指定列抓住数据,并尝试设置为列的绑定。这使我同时拥有列名和所有进入该列中的数据的列表。到列结束了不显示任何数据不幸绑定数据的列表。

This looks at the first entry an extracts the Column Header values and creates new columns based on that. The binding statement looks at each Row, grabs data for the specified Column, and tries to set that as the Column's Binding. This allows me to have both the column name and a list of all the data that goes in that column. Unfortunately binding the list of data to the column ends up not displaying any data.

所以我的问题是:如何做我需要格式化我的数据,以便为列绑定到实际显示的数据?我在处理这个情况都错了?任何帮助,我感到很为难AP preciated。

So my question is: How do I need to format my data in order for the Column Binding to actually display data? Am I approaching this situation all wrong? Any help is appreciated as I am quite stumped.

推荐答案

下面是一个完整的工作示例

Here's a complete working sample

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var data = new MyViewModel();

        int columnIndex = 0;

        foreach (var name in data.ColumnNames)
        {
            grid.Columns.Add(
                new DataGridTextColumn
                {
                    Header = name,
                    Binding = new Binding(
                        string.Format("Values[{0}]", columnIndex++))
                });
        }

        DataContext = data;
    }
}

public class MyViewModel
{
    public MyViewModel()
    {
        Items = new List<RowDataItem>
            {
                new RowDataItem(),
                new RowDataItem(),
                new RowDataItem(),
            };

        ColumnNames = new List<string>{ "Col 1", "Col 2", "Col 3"};
    }

    public IList<string> ColumnNames { get; private set; }

    public IList<RowDataItem> Items { get; private set; }
}

public class RowDataItem
{
    public RowDataItem()
    {
        Values = new List<string>{ "One", "Two", "Three"};    
    }

    public IList<string> Values { get; private set; }
}

XAML中

<Grid>
    <DataGrid x:Name="grid" ItemsSource="{Binding Items}" 
              AutoGenerateColumns="False"/>
</Grid>

这篇关于一个DataGridColumn动态生成的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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