コード例 #1
0
        public TypedListViewDisplayMember Clone()
        {
            TypedListViewDisplayMember res = new TypedListViewDisplayMember();

            res._defaultStyles           = this._defaultStyles;
            res.BackColor                = this.BackColor;
            res.Checked                  = this.Checked;
            res.CustomAttributeType      = this.CustomAttributeType;
            res.CustomAttributeValueName = this.CustomAttributeValueName;
            res.DataSourceName           = this.DataSourceName;
            res.DisplayMember            = this.DisplayMember;
            res.Caption                  = this.Caption;
            //res.SubItems = this.SubItems;
            res.SubItems = new BaseListViewItemsCollection();
            foreach (var item in this.SubItems)
            {
                BaseListViewItem newitem = new BaseListViewItem(item.DisplayMember, item.DataSourceName);
                newitem.Caption                  = item.Caption;
                newitem.BackColor                = item.BackColor;
                newitem.CustomAttributeType      = item.CustomAttributeType;
                newitem.CustomAttributeValueName = item.CustomAttributeValueName;
                newitem.Font                  = item.Font;
                newitem.ForeColor             = item.ForeColor;
                newitem.ImageIndexByFieldName = item.ImageIndexByFieldName;
                res.SubItems.Add(newitem);
            }

            return(res);
        }
コード例 #2
0
        /// <summary>
        /// Метод заполняющий контролс типа ListView
        /// </summary>
        /// <param name="ctrl">Сам контрол</param>
        /// <param name="DataSource">Источник данных</param>
        /// <param name="ItemDesc">Описание элементов листвью</param>
        public static void FillListView(ListView ctrl, object DataSource, TypedListViewDisplayMember ItemDesc)
        {
            if (ItemDesc != null)
            {
                ListViewItem li = new ListViewItem();

                #region IEnumerable Case
                if ((DataSource is System.Collections.IEnumerable) && DataSource.GetType() != typeof(string))
                {
                    var collection = (DataSource as System.Collections.IEnumerable);
                    foreach (var item in collection)
                    {
                        object text;
                        if (/*item.GetType().GetProperty(*/ ItemDesc.DisplayMember == null && ItemDesc.DataSourceName != null)
                        {
                            if (!item.GetType().IsClass)
                            {
                                text = item;
                            }
                            else
                            {
                                var val = item.GetType().GetProperty(ItemDesc.DataSourceName).GetValue(item, null);
                                text = val.GetType().GetProperty(ItemDesc.DisplayMember).GetValue(val, null);
                            }
                        }
                        else
                        {
                            if (item.GetType() != typeof(string))
                            {
                                text = item.GetType().GetProperty(ItemDesc.DisplayMember).GetValue(item, null);
                            }
                            else
                            {
                                text = item.ToString();
                            }
                            //--тут надо передалть и убрать тип аттрибута!!!
                            if (ItemDesc.CustomAttributeType != null)
                            {
                                var custattrs = ItemDesc.CustomAttributeType.GetField(text.ToString()).GetCustomAttributes(false);
                                if (custattrs != null)
                                {
                                    foreach (var attr in custattrs)
                                    {
                                        var text2 = attr.GetType().GetProperty(ItemDesc.CustomAttributeValueName).GetValue(attr, null);
                                        if (text2 != null)
                                        {
                                            text = text2;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        if (text != null)
                        {
                            li         = new ListViewItem(text.ToString());
                            li.Checked = ItemDesc.Checked;
                            if (!ItemDesc.IsDefaultSttyle())
                            {
                                li.Font      = ItemDesc.Font;
                                li.ForeColor = ItemDesc.ForeColor;
                                li.BackColor = ItemDesc.BackColor;
                            }
                            if (string.IsNullOrEmpty(ItemDesc.ImageIndexByFieldName))
                            {
                                li.ImageIndex = ItemDesc.ImageIndex;
                            }
                            else
                            {
                                var im_idx = item.GetType().GetProperty(ItemDesc.ImageIndexByFieldName).GetValue(item, null);
                                if (im_idx != null && im_idx.GetType() == typeof(int))
                                {
                                    li.ImageIndex = (int)im_idx;
                                }
                            }
                            li.Tag = item;
                        }

                        if (ItemDesc.SubItems.Count > 0)
                        {
                            foreach (var subitem in ItemDesc.SubItems)
                            {
                                object text2 = null;
                                if (subitem.DataSourceName != null)
                                {
                                    if (item.GetType().GetProperty(subitem.DataSourceName) != null)
                                    {
                                        if (!item.GetType().IsClass)
                                        {
                                            text2 = item;
                                        }
                                        else
                                        {
                                            var val = item.GetType().GetProperty(subitem.DataSourceName).GetValue(item, null);
                                            if (val != null)
                                            {
                                                text2 = val.GetType().GetProperty(subitem.DisplayMember).GetValue(val, null);
                                            }
                                        }
                                    }
                                    else //--экспириметально
                                    {
                                        if (!item.GetType().IsClass)
                                        {
                                            text2 = item;
                                        }
                                        else
                                        {
                                            var val = item.GetType().GetProperty(subitem.DisplayMember).GetValue(item, null);
                                            if (val != null && (val.GetType() != typeof(string) & val.GetType() != typeof(DateTime) & val.GetType() != typeof(int)))
                                            {
                                                text2 = val.GetType().GetProperty(subitem.DisplayMember).GetValue(val, null);
                                            }
                                            else
                                            {
                                                text2 = val;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    text2 = item.GetType().GetProperty(subitem.DisplayMember).GetValue(item, null);
                                    if (subitem.CustomAttributeType != null)
                                    {
                                        var custattrs = subitem.CustomAttributeType.GetField(text2.ToString()).GetCustomAttributes(false);
                                        if (custattrs != null)
                                        {
                                            foreach (var attr in custattrs)
                                            {
                                                var text3 = attr.GetType().GetProperty(subitem.CustomAttributeValueName).GetValue(attr, null);
                                                if (text3 != null)
                                                {
                                                    text2 = text3;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                                if (text2 != null)
                                {
                                    ListViewItem.ListViewSubItem si = new ListViewItem.ListViewSubItem();
                                    si.Text = text2.ToString();
                                    if (!subitem.IsDefaultSttyle())
                                    {
                                        si.Font      = ItemDesc.Font;
                                        si.ForeColor = ItemDesc.ForeColor;
                                        si.BackColor = ItemDesc.BackColor;
                                    }
                                    li.SubItems.Add(si);
                                }
                            }
                        }

                        ctrl.Items.Add(li);
                    }
                }
                #endregion

                //--надо сделать еще классами, а не только коллекции!!!

                #region DataTable Case
                if (DataSource is DataTable)
                {
                    var table = (DataSource as DataTable);
                    //--captions:cap1;cap2;cap3...
                }
                #endregion
            }
        }