首页 > 代码库 > Silverlight系列--动态生成DataGrid列 根据动态列绑定Dictionary数据
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端会出错的。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。