在 C# WPF 中使用 Datacontext 填充数据网格

发布于 2024-09-02 00:57:39 字数 1390 浏览 1 评论 0原文

我正在尝试从文件中获取数据。该文件首先有三行描述该文件的文本,然后是下面数据的标题。我已经设法提取它。我遇到的问题是获取标题行下方的数据。标题行下方的数据可能类似于“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;”然后我在正确的列中获取数据,但我也得到一些原始视图数据,例如:比较器、计数、键、值作为列,这是我不想要的。

有什么见解吗?

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;

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.

Any insight?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身骄傲 2024-09-09 00:57:39

当您使用 DataContext 时WPF 中的控件上的属性,用于设置控件在填充具有与其关联的绑定的属性时将使用的数据源。

这与设置 ItemsSource 属性 指示您希望在网格中显示的数据。是的,仍然会使用绑定,但它们以不同的方式用于网格中显示的数据,而不是用于驱动网格本身配置的数据(这就是 DataContext 确实)。

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

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.

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).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文