示例#1
0
        private GridView ConstructGridView(object item)
        {
            var view = new GridView();

            bool isBaseType = IsBaseType(item);

            if (isBaseType || HideColumnHeaders)
            {
                view.ColumnHeaderContainerStyle = ZeroHeightColumnHeader;
            }

            if (isBaseType)
            {
                var gvc = new GridViewColumn();
                gvc.Header = item.GetType().Name;

                gvc.DisplayMemberBinding = new Binding();
                view.Columns.Add(gvc);

                return(view);
            }

            if (ExplicitlyIncludeColumns && Columns != null && Columns.Count > 0)
            {
                foreach (var col in Columns.Where(c => !c.Hide).OrderBy(c => c.Position ?? int.MaxValue))
                {
                    view.Columns.Add(CloneHelper.Clone(col));
                }
                return(view);
            }

            var typeProperties = (from p in item.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                  where p.CanRead && p.CanWrite
                                  select new {
                Name = p.Name,
                Column = Columns.FirstOrDefault(c => p.Name.Equals(c.ForProperty))
            }).ToList();

            //This is a shortcut to sort the nulls to the back of the list instead of the front
            //we did this instead of creating an IComparer.
            var typesWithPositions = typeProperties
                                     .Where(a => a.Column != null && a.Column.Position != null).OrderBy(a => a.Column.Position);

            var typesWithoutPositions = typeProperties.Except(typesWithPositions);

            var sortedProperties = typesWithPositions.Concat(typesWithoutPositions);

            foreach (var currentProperty in sortedProperties)
            {
                if (currentProperty.Column != null)
                {
                    if (!currentProperty.Column.Hide)
                    {
                        var gvc = CloneHelper.Clone(currentProperty.Column);

                        if (gvc.Header == null)   // TODO check if this is bound to anything
                        {
                            gvc.Header = currentProperty.Name;
                        }

                        if (gvc.DisplayMemberBinding == null)
                        {
                            gvc.DisplayMemberBinding = new Binding(currentProperty.Name);
                        }

                        view.Columns.Add(gvc);
                    }
                }
                else
                {
                    var gvc = new GridViewColumn();
                    gvc.Header = currentProperty.Name;

                    gvc.DisplayMemberBinding = new Binding(currentProperty.Name);
                    view.Columns.Add(gvc);
                }
            }

            return(view);
        }