无法在WPF中的DataGrid中显示列表项 [英] Unable to display list items in DataGrid in WPF
本文介绍了无法在WPF中的DataGrid中显示列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了一个类 DisplayTable
,下面将对此进行介绍。
I have created the class DisplayTable
, which was mentioned below.
我还创建了一个列表来将类的内容添加到列表中。
I have also created one list to add the contents of the class to the list.
我将填充的列表推到了DataGrid中。但是我无法在DataGrid中查看它们。
I pushed the populated list to the DataGrid. But I'm unable to view those in the DataGrid. Kindly help in fixing this.
public class DisplayTable
{
public int AnalyteId;
public int UnitCode;
public int ReferenceValue;
}
private void btnAddAnalyte_Click(object sender, RoutedEventArgs e)
{
DisplayTable d = new DisplayTable();
List<DisplayTable> list = new List<DisplayTable>();
foreach (CheckBox item in this.AnalyteLitst.Items)
{
if (item.IsChecked == true)
{
d.AnalyteId = 1;
}
}
foreach (CheckBox unit in this.UnitsList.Items)
{
if (unit.IsChecked == true)
{
d.UnitCode = 12;
}
}
list.Add(d);
dataGrid.AutoGenerateColumns = true;
dataGrid.IsReadOnly = false;
dataGrid.RowHeight = 30;
dataGrid.ColumnWidth = 100;
dataGrid.ItemsSource = list;
}
推荐答案
DataGrid仅自动为属性,因为每个列都会创建一个绑定,而WPF绑定需要 properties 。 DisplayTable
类声明 fields 。
DataGrid generates columns automatically only for properties, because each column creates a binding and WPF bindings require properties. DisplayTable
class declares fields.
而不是
public int AnalyteId;
make
public int AnalyteId { get; set; }
并以相同方式修复其他数据成员
and fix other data members in the same way
这篇关于无法在WPF中的DataGrid中显示列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文