標籤:class blog code tar ext get
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端會出錯的。