您好我是C#和WPF的新手,我想在数据网格中显示一个用户对象列表(PartnerID和PartnerName)以及一个询问是否删除Partner的复选框。我编写了以下代码,但只显示列标题,并且不显示值。我认为它的绑定问题,但我无法追踪它。你能帮助我吗?
private void Window_Loaded(object sender, RoutedEventArgs e)
{
List<Partners> partners = new List<Partners>();
DataAccess DA = new DataAccess();
partners = DA.GetAllPartners();
this.dataGrid1.DataContext = partners;
this.dataGrid1.Columns.Add(new DataGridTextColumn { Header = "Id", Binding new Binding("PartnerId") });
this.dataGrid1.Columns.Add(new DataGridTextColumn { Header = "Partner Name", Binding = new Binding("PartnerName") });
this.dataGrid1.Columns.Add(new DataGridCheckBoxColumn
{
Header = "Delete?",
Binding = new Binding(" ")
});
}
提前致谢
答案 0 :(得分:4)
尝试设置DataGrid的ItemSource
属性而不是DataContext
...
dataGrid1.ItemsSource = partners;
在一个不相关的说明中但与WPF新手相关 - 请查看MVVM(Model-View-ViewModel)设计模式。它将帮助您以更易于维护的方式组织此类代码。