WPF应用程序-无法显示数据网格中列表中的数据 [英] WPF Application - can't display data from a list in a datagrid

查看:86
本文介绍了WPF应用程序-无法显示数据网格中列表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用WPF和c#的新手,并且遇到了(可能非常简单)的问题。

I'm a newbie with WPF and c# and have a (probably very simple) problem.

我正在编写一个小型应用程序,我正在尝试从数据库中读取数据,将其格式化为列表,并显示在数据网格中。我已经调试了它,并成功读取了数据库,所有数据都在列表中( cd_list ),但是它没有将数据传递给datagrid。

I'm coding a small application, and i'm trying to read data from a database, format it into a list, and display in a datagrid. Ive debugged it, and it I've successfully read the database, and all the data is in the list (cd_list), but it's not passing the data to the datagrid.

以下是我的 XAML

<DataGrid Name="DataGrid" AutoGenerateColumns="false" RowHeaderWidth="0" Width="240" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" ></DataGridTextColumn>
                <DataGridTextColumn Header="Details"></DataGridTextColumn>
                <DataGridTextColumn Header="Employee"></DataGridTextColumn>
                <DataGridTextColumn Header="Date"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>

我在文件后面的代码执行 InitializeComponent ,然后做一些工作来从数据库中获取数据,并将其放在 call_detail 对象的列表中:

My code behind file does InitializeComponent, then does some work to get the data from a DB, and put it in a list of call_detail objects:

public class call_details
{
    public string name;
    public string details;
    public string employee;
    public string date;

    public call_details()
    {
    }
}

声明列表&呼叫详细信息对象

Declare list & call detail objects

   public call_details cd_rec = new call_details();
   public List<call_details> cd_list = new List<call_details>();

将记录添加到列表中

   cd_list.Add(cd_rec);           

完成此操作后,我尝试提供ItemsSource,以便数据显示在Datagrid中,并具有以下内容:

After this is done, I am trying to give the ItemsSource so that the data will show in the Datagrid, and have the following:

DataGrid.ItemsSource = cd_list;

,但它不起作用。在GUI中,Datagrid显示了一个网格,其中包含我期望的正确数量的记录,但是它们都是空白的,所以我猜它正在传递一些信息,而不是实际数据。
这可能是一个愚蠢的错误,由于是新手而犯了,但是我找不到其他可以帮助我的东西。

but its not working. In the GUI, the Datagrid shows a grid, with the correct number of records that I expect, but they are all blank.So I guess that it is passing some info, just not the actual data. It's probably a silly mistake which I have made due to being a novice, but I can't find anything else out there to help me.

有人吗?

推荐答案

所有这些字段都必须使用至少一个吸气剂公开为公共属性(这是绑定系统的要求)。然后,您需要绑定到属性或只使用自动生成的列。

All those fields need to be exposed as public properties with at least a getter (this is a requirement of the binding-system). Then you need to bind to the properties or just use the auto-generated columns.

public string Name { get; private set; }





<DataGridTextColumn Header="Name" Binding="{Binding Name}" />

如果您不熟悉WPF,我强烈建议您阅读一些MSDN上的文章。例如数据绑定概述

If you are new to WPF i highly recommend reading some of the articles on MSDN. e.g. the Data Binding Overview.

这篇关于WPF应用程序-无法显示数据网格中列表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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