2010-11-09 54 views
1
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

什麼是錯誤您收到? – 2010-11-09 12:57:28

+0

除了格式之外,沒有什麼直接的錯誤。會發生什麼,它與你的預期有什麼不同? – 2010-11-09 12:59:00

+0

該代碼生成2行到數據網格,但它們是空的 – Fulano 2010-11-09 13:06:03

回答

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 = ""; } 
+0

謝謝,但它仍然無法正常工作。數據網格仍然是空的 – Fulano 2010-11-09 13:07:21

+0

我添加了這個並且工作。謝謝。 Fulano 2010-11-09 13:24:59