//当不想创建类,或者这个类的字段是动态变化的(不确定有多少字段)
//可以考虑动态对象,方便好用
//此处示例是用DataGrid用的数据源动态绑定
ObservableCollection<dynamic> dataSource = new ObservableCollection<dynamic>();
// 创建动态对象
dynamic dynamicObject = new ExpandoObject();
// 获取动态对象的字典
var row = (IDictionary<string, object>)dynamicObject;
row.Add("Name",itemRow.Name);//点位名称;
for (int i = 0; i < PointList.Count; i++)
{
//动态名称 V1 V2 V3....
row.Add("V"+i.ToString(),PointList[i].ToString("N3"));
}
dataSource.Add(row);
DataGrid.ItemsSource = dataSource;
//最后就能这样使用了
var name = row.Name;
var V1 = row.V1;
var V2 = row.V2;
//也能用作界面数据绑定
Columns.Add(new DataGridTextColumn() { Width=100, IsReadOnly=false, Header="点位名称",Binding=new Binding("Name")});
Columns.Add(new DataGridTextColumn() { Width=100, IsReadOnly=false, Header="点位名称",Binding=new Binding("V"+i)});
//快去你的项目中试试吧
标签:Name,灵活,C#,绑定,Binding,Add,var,new,row From: https://blog.51cto.com/u_16544892/9437037