Silverlight系列--動態產生DataGrid列 根據動態資料行繫結Dictionary資料

來源:互聯網
上載者:User

標籤: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端會出錯的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.