在C#WPF中使用Datacontext填充datagrid [英] Populate datagrid using Datacontext in C# WPF

查看:210
本文介绍了在C#WPF中使用Datacontext填充datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文件中获取数据。该文件首先包含三行描述文件的文本,然后有下面的数据标题。我设法将其提取出来。我遇到的问题是使数据低于标题行。标题行下方的数据看起来像 1 2 3 4 5 6 7 8 9 abc。

I'm trying to get data from file. The file first has three lines of text that describes the file, then there is a header for the data below. I've managed to extract that. What I'm having problems is getting the data below the header line. The data below the header line can look like "1 2 3 4 5 6 7 8 9 abc".

DataGrid dataGrid1 = new DataGrid();
masterGrid.Children.Add(dataGrid1);

using (TextReader tr = new StreamReader(@filename))
{
    int lineCount = 1;
    while (tr.Peek() >= 0)
    {
        string line = tr.ReadLine();
        {
            string[] data = line.Trim().Split(' ');

            Dictionary<string, object> sensorData = new Dictionary<string, object>();

            for (int i = 0, j = 0; i < data.Length; i++)
            {
                //I know that I'm delimiting the data by a space before.
                //but the data from the text file doesn't necessarily have
                //just one space between each piece of data
                //so if I don't do this, spaces will become part of the data.
                if (String.Compare(data[i]," ") > 0)
                {
                    sensorData[head[j]] = data[i];
                    j++;
                }
            }

            sensorDatas.Add(sensorData);

            sensorData = null;

        }
        lineCount++;
    }
}

dataGrid1.DataContext = sensorDatas;

我不知道为什么这样不起作用。如果我更改 dataGrid1.DataContext = sensorDatas;到 dataGrid1.ItemsSource = sensorDatas;然后我在适当的列中获取了数据,但是我也获得了一些Raw View数据,例如:比较器,计数,键,值作为列,这些都是我所不希望的。

I can't figure out why this doens't work. If I change "dataGrid1.DataContext = sensorDatas;" to "dataGrid1.ItemsSource = sensorDatas;" then I get the data in the proper columns, but I also get some Raw View data such as: Comparer, Count, Keys, Values as columns, which I don't want.

有见识吗?

推荐答案

使用 DataContext 属性在WPF中,即

When you use the DataContext property on a control in WPF, that is used to set the data source that the control will use when populating properties which have bindings associated with them.

用于设置控件在填充与它们相关联的属性时将使用的数据源。设置 ItemsSource 属性,以指示您希望在网格中显示的数据。是的,绑定仍将使用,但是它们以不同的方式用于网格中显示的数据,而不是用于驱动网格本身配置的数据( DataContext 确实)。

This is a very different thing than setting the ItemsSource property to indicate the data you wish to show in the grid. Yes, bindings will still be used, but they are used in a different way for the data that is shown in the grid, not the data that is used to drive the configuration of the grid itself (which is what DataContext does).

相反,似乎您允许网格为您自动生成列。而是应指示应关闭列的自动生成,然后设置要显示的列。然后,当您设置 ItemsSource 时,它应该仅显示您希望显示的项目。

Rather, it seems that you are allowing the grid to auto-generate the columns for you. Instead, indicate that the auto-generation of the columns should be shut off, and then set the columns you wish to display. Then, when you set the ItemsSource, it should show only the items you wish to show.

这篇关于在C#WPF中使用Datacontext填充datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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