C# 如何将对象列表绑定到datagrid?
首先,我已经阅读了所有微笑的问题,并尝试了他们的答案。对我来说似乎什么都不管用 我创建了一个名为C# 如何将对象列表绑定到datagrid?,c#,wpf,C#,Wpf,首先,我已经阅读了所有微笑的问题,并尝试了他们的答案。对我来说似乎什么都不管用 我创建了一个名为student的类。然后我从数据库中加载了一个学生列表 现在,我希望在datagrid中显示此列表。我得到的只是一张空桌子( 出现了什么问题?(我尝试了datacontext而不是itemSource) 我的C#代码: 公共部分类主窗口:窗口 { 公共列表学生列表=新列表(); 公共主窗口() { 尝试 { 初始化组件(); Classes.studentManager stdManager=新类.s
student
的类。然后我从数据库中加载了一个学生列表
现在,我希望在datagrid中显示此列表。我得到的只是一张空桌子(
出现了什么问题?(我尝试了datacontext而不是itemSource)
我的C#代码:
公共部分类主窗口:窗口
{
公共列表学生列表=新列表();
公共主窗口()
{
尝试
{
初始化组件();
Classes.studentManager stdManager=新类.studentManager();
studentsList=stdManager.Select();
this.dgstusts.DataContext=studentsList.ToList();
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
}
XAML代码如下
<DataGrid AutoGenerateColumns="False" Height="211" Name="dgStudents" Width="Auto" Margin="39,0,-33,-142" VerticalAlignment="Bottom" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Header="birthDate" Width="175" Binding="{BindingfirstName}" />
</DataGrid.Columns>
</DataGrid>
这是学生的目标
public class student
{
public int ID;
public string firstName;
public string lastName;
public string studentID;
public string homePhone;
public string cellPhone;
public string parentsPhone;
public string parentsName;
public string adress;
public bool isPicFormExists;
public string medProblems;
public bool isParentsConfExists;
public List<Class> classesList;
public DateTime birthDate;
public List<payments> payments;
public double accountBalance;
public student()
{
try
{
}
catch (Exception ex)
{
//TO-DO
}
}
}
公共班级学生
{
公共int ID;
公共字符串名;
公共字符串lastName;
公共字符串学生ID;
公共字符串家庭电话;
公共字符串手机;
公共字符串parentsPhone;
公共字符串parentsName;
公共字符串地址;
公共bool isPicFormExists;
公共卫生问题;
公共利益是不存在的;
公共列表类列表;
公共日期时间出生日期;
公开名单付款;
公共双账户余额;
公立学生()
{
尝试
{
}
捕获(例外情况除外)
{
//待办事项
}
}
}
您需要在学生
类中定义公共属性。您不能绑定到字段
WPF支持绑定到对象的属性,而不是字段
有关更多信息,请参见此问题:您需要在
学生
类中定义公共属性。您不能绑定到字段
WPF支持绑定到对象的属性,而不是字段
查看此问题了解更多信息:学生类中是否有属性或变量?此语句的作用是“studentsList=stdManager.Select();”?感谢您的回答。Andrey已经解决了此问题。正如nit所建议的,我将其更改为属性…学生类中是否有属性或变量?此语句的作用是什么“studentsList=stdManager.Select();”?感谢您的回复。Andrey已经解决了这个问题。正如nit所建议的,我将其更改为属性。。。
public class student
{
public int ID;
public string firstName;
public string lastName;
public string studentID;
public string homePhone;
public string cellPhone;
public string parentsPhone;
public string parentsName;
public string adress;
public bool isPicFormExists;
public string medProblems;
public bool isParentsConfExists;
public List<Class> classesList;
public DateTime birthDate;
public List<payments> payments;
public double accountBalance;
public student()
{
try
{
}
catch (Exception ex)
{
//TO-DO
}
}
}