WPF中DataGrid绑定List<T>怎么没成功?
- C# code
namespace XXXXX{ struct sREG { public DateTime REGDatetime; public string REGENo; public string EName; public bool REGRed; public string REGStyle; public double REGCost; } public partial class REquipmentG : Window { List<sREG> _listREG = new List<sREG>(); //……………………………… //……………………………… //……………………………… //初始化DataGrid的函数 private bool InitialDataGrid() { try { SqlDataReader reader = null; //CommonFunction.SQLSearch此函数返回个Reader CommonFunction.SQLSearch("select REGDatetime,REGENo,EName,REGRed,REGStyle,REGCost from tEquipments,tREG where tREG.REGENo=tEquipments.ENo", ref MainWindow._cnn, ref reader); while (reader.Read()) { sREG tempsREG = new sREG(); tempsREG.REGDatetime = Convert.ToDateTime(reader[0]); tempsREG.REGENo = Convert.ToString(reader[1]); tempsREG.EName = Convert.ToString(reader[2]); tempsREG.REGRed = Convert.ToBoolean(reader[3]); tempsREG.REGStyle = Convert.ToString(reader[4]); tempsREG.REGCost = Convert.ToDouble(reader[5]); _listREG.Add(tempsREG); } dgMain.ItemsSource = _listREG; } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } //MessageBox.Show(_listREG[0].EName); return true; } }}
- XML code
<Canvas > <DataGrid x:Name="dgMain" Width="400" Height="300"/> </Canvas>
为什么DataGrid什么内容页不显示?
[解决办法]
struct sREG
{
public DateTime REGDatetime;
public string REGENo;
public string EName;
public bool REGRed;
public string REGStyle;
public double REGCost;
}
绑定只对属性有效,都改成属性