Silverlight系列--动态生成DataGrid列 根据动态列绑定Dictionary数据
1.获取表头数据源动态生成DataGrid表头
DataGridTextColumn d = new DataGridTextColumn(); d.Header = itemPriceClass.PriceKindCode + itemPriceClass.PriceKindName; Binding bin = new Binding(); bin.Converter = new RowIndexConverter(); bin.ConverterParameter = itemPriceClass.PriceKindCode; d.Binding = bin; d.CanUserSort = false; d.FontSize = 13; d.IsReadOnly = true; dgList.Columns.Add(d);
注:其中Binding这一段是对该列的数据绑定处理的,由于数据源是Dictionary中获取,所以要对数据显示部分进行处理。
public class RowIndexConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { Row row = value as Row; string index = parameter as string; return row[index]; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
注:最终DataGrid的数据源部分就是List<Row>
public class Row { Dictionary<string, object> dictionary = new Dictionary<string, object>(); public object this[string index] { get { if (dictionary.ContainsKey(index)) { return dictionary[index]; } else { return 0; } } set { dictionary[index] = value;} } }
2.对数据源进行处理
得到的数据源格式
inpareaname pricekindname realcost pricekindcode
A ca 100 101
A ba 150 102
B da 100 104
注:pricekindname就是表头显示的数据
private List<Row> CreateDataGridColumn() { List<Row> result = new List<Row>(); if (itemPriceClassList != null && itemPriceClassList.Count > 0) { if (wardExpenseDetailByPriceItemClassList != null && wardExpenseDetailByPriceItemClassList.Count > 0) { foreach (WardExpenseDetailByPriceItemClass detail in wardExpenseDetailByPriceItemClassList) { if (result.Count > 0) { bool flag = false; foreach (Row r in result) { if (r["AreaName"].ToString() == detail.InpAreaName) { r[detail.PriceKindCode] = detail.RealCost; flag = true; break; } } if (!flag) { Row row = new Row(); row["AreaName"] = detail.InpAreaName; row[detail.PriceKindCode] = detail.RealCost; result.Add(row); } } else { Row row = new Row(); row["AreaName"] = detail.InpAreaName; row[detail.PriceKindCode] = detail.RealCost; result.Add(row); } } } } rowDataList = result; return result; }
注:还有一种解决方案---未测试
1.在silverlight的service层中动态生成实体类,其中属性为表头的值,service层动态拼接成List<T>传到client端。
注:object类型传到client端会出错的。
Silverlight系列--动态生成DataGrid列 根据动态列绑定Dictionary数据,古老的榕树,5-wow.com
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。