public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Person> lista = new List<Person>();
lista.Add(new Person(1, "Joao", 50.0f));
lista.Add(new Person(2, "Maria", 150.0f));
dataGrid1.ItemsSource = lista;
}
public class Person
{
public int id;
public string name;
public float salary;
public Person(int id, string name, float salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
}
}
答案 0 :(得分:1)
绑定通常是属性,而不是字段:
public int Id {get;set;}
public string Name {get;set;}
public decimal Salary {get;set;}
public Person(int id, string name, decimal salary)
{
Id = id;
Name = name;
Salary = salary;
}
请注意 - Salary
当然应该是decimal
(不是float
)。
如果您发现无法创建新Person
条记录的行,请尝试添加无参数构造函数:
public Person() { Name = ""; }