Exemplo n.º 1
0
        public void RenderGrid()
        {
            var stream = new ColloredStringBuilder();

            SourceList.CollectionChanged -= SourceListOnCollectionChanged;

            //For Native type Support we need to wrap the Type into a Anonymos one



            if (ObserveList)
            {
                SourceList.CollectionChanged += SourceListOnCollectionChanged;
            }

            var size   = 0;
            var fod    = SourceList.FirstOrDefault();
            var length = SourceList.Count;

            if (fod == null)
            {
                return;
            }

            Target = fod.GetType();
            if (Target.IsPrimitive)
            {
                SourceList = new ObservableCollection <object>(SourceList.Select(s => new { s }));
                Target     = fod.GetType();
            }

            var props =
                Target.GetProperties().Select(s =>
            {
                var name = s.Name;
                if (RenderTypeName)
                {
                    name = string.Format("{0} {{{1}}}", name, s.PropertyType.ToString());
                }

                var valueInformations = new ValueInformations()
                {
                    GetValue       = s.GetValue,
                    MaxContentSize = SourceList.Max(e =>
                    {
                        var value = s.GetValue(e);
                        if (value != null)
                        {
                            return(value.ToString().Length);
                        }
                        return(Null.ToString().Length);
                    }),
                    Name = name,
                };

                return(valueInformations);
            }).ToList();


            if (RenderRowNumber)
            {
                int fakeId = 0;
                //fake new Column
                props.Insert(0, new ValueInformations()
                {
                    Name           = "Nr",
                    MaxContentSize = length.ToString().Length,
                    GetValue       = o => fakeId++
                });
            }

            foreach (var valueInfo in props)
            {
                valueInfo.Name = AlignValueToSize(valueInfo.Name, valueInfo.MaxSize);
                size          += valueInfo.Name.Length;
            }

            var headerInfos = props.Select(s => s.Name).ToList();

            this.ConsolePropertyGridStyle.RenderHeader(stream, headerInfos);

            stream.AppendLine();

            if (Console.WindowWidth < size)
            {
                Console.WindowWidth = size + 30;
            }

            for (int i = 0; i < length; i++)
            {
                var item = SourceList[i];

                var selected = SelectedItems != null && SelectedItems.Contains(item);
                var focused  = FocusedItem == item;

                this.ConsolePropertyGridStyle.BeginRenderProperty(stream, i, length.ToString().Length, selected, focused);

                for (int index = 0; index < props.Count; index++)
                {
                    var propertyInfo = props[index];
                    var value        = propertyInfo.GetValue(item) ?? Null;
                    var norm         = AlignValueToSize(value.ToString(), propertyInfo.MaxSize);
                    this.ConsolePropertyGridStyle.RenderNextProperty(stream, norm, index, selected, focused);
                }
                this.ConsolePropertyGridStyle.EndRenderProperty(stream, i, selected, focused);
                stream.AppendLine();
            }


            this.ConsolePropertyGridStyle.RenderFooter(stream, headerInfos);
            stream.AppendLine();

            if (RenderSum)
            {
                this.ConsolePropertyGridStyle.RenderSummary(stream, length);
                stream.AppendLine();
            }

            if (_extraInfos.Length > 0)
            {
                this.ConsolePropertyGridStyle.RenderAdditionalInfos(stream, _extraInfos);
                stream.AppendLine();
            }

            if (!PersistendAdditionalInfos)
            {
                _extraInfos.Clear();
            }

            if (ClearConsole)
            {
                Console.Clear();
            }
            stream.Render();
        }