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;
}
}
}
1
A
回答
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 = ""; }
相關問題
- 1. 數據綁定到一個數組集合不工作
- 2. 數據綁定不工作
- 3. 數據綁定不工作
- 4. 數據綁定到一個窗口「的財產不工作
- 5. wpf dataGrid綁定selecteditem不工作
- 6. WPF嵌套的DataGrid綁定不工作
- 7. 將DataGrid列綁定到SQL數據庫不起作用
- 8. ItemsControl的數據綁定...綁定到當前項目工作不
- 9. 綁定兩個數據庫表到同一Datagrid的
- 10. 數據表沒有被綁定到DataContext的一個DataGrid
- 11. 應該DataGrid和數據形綁定到PagedCollectionView或一個ObservableCollection
- 12. 雙向wpf datagrid綁定到數據庫
- 13. Datagrid的數據綁定到XML與DataGridTemplateColumn
- 14. WPF DataGrid綁定到數據表
- 15. 綁定分級數據模型到DataGrid
- 16. 如何將此數據綁定到DataGrid?
- 17. XAML DataGrid - RowDetail綁定到主行數據
- 18. 如何將ItemsSource綁定到DataGrid時將數據綁定到DataGridTemplateColumn?
- 19. Datagrid的數據綁定
- 20. 數據綁定DataGrid中
- 21. WPF datagrid綁定數據表
- 22. WPF DataGrid RowHeader數據綁定
- 23. WPF綁定到一個DataGrid的的SelectedItem
- 24. 綁定WPF DataGrid列到另一個
- 25. 不能綁定到datagrid?
- 26. WPF數據綁定到BOOL未工作
- 27. 綁定不與DataGrid上的行標題一起工作
- 28. 數據綁定到一個數據行
- 29. WPF數據綁定工作不正常
- 30. UWP - 綁定數據不工作 - 空ListView
什麼是錯誤您收到? – 2010-11-09 12:57:28
除了格式之外,沒有什麼直接的錯誤。會發生什麼,它與你的預期有什麼不同? – 2010-11-09 12:59:00
該代碼生成2行到數據網格,但它們是空的 – Fulano 2010-11-09 13:06:03