Ejemplo n.º 1
0
        public static void UpdatePropertyGrid(this PropertyGrid pg)
        {
            var expandedGridItemPaths = new HashSet <string>(
                pg
                .GetAllGridEntries()
                .Cast <GridItem>()
                .Where(gi1 => gi1.Expanded)
                .Select(gi1 => string.Join("/", gi1.GetHierarchy().Select(gi2 => gi2.Label))));

            var allItems = pg.GetAllGridEntries()
                           .Cast <GridItem>()
                           .ToList();

            var selectedGridItemIndex = allItems.IndexOf(pg.SelectedGridItem);

            var selectedGridItemPath = string.Join("/", pg.SelectedGridItem.GetHierarchy().Select(gi2 => gi2.Label));

            var view      = pg.GetPropertyGridView();
            var scrollBar = (ScrollBar)view.GetType().GetField("scrollBar", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(view);
            var scroll    = scrollBar.Value;

            var oldSelected = pg.SelectedObject;

            pg.SelectedObject = null;
            pg.SelectedObject = oldSelected;

            var newExpandableGridItems = new Queue <GridItem>(
                pg
                .GetAllGridEntries()
                .Cast <GridItem>()
                .Where(gi => gi.Expandable || gi.GridItems.Count > 0));

            bool anySelected = false;

            while (newExpandableGridItems.Count > 0)
            {
                var gi = newExpandableGridItems.Dequeue();

                var path = string.Join("/", gi.GetHierarchy().Select(gi1 => gi1.Label));

                if (expandedGridItemPaths.Contains(path))
                {
                    gi.Expanded = true;
                }

                if (path == selectedGridItemPath)
                {
                    gi.Select();
                    anySelected = true;
                }

                foreach (var child in gi.GridItems.Cast <GridItem>())
                {
                    newExpandableGridItems.Enqueue(child);
                }
            }

            if (!anySelected)
            {
                var allItems2 = pg.GetAllGridEntries()
                                .Cast <GridItem>()
                                .ToList();

                var index = Math.Min(selectedGridItemIndex, allItems2.Count);
                allItems2[index].Select();
            }

            scrollBar.Value = scroll;
        }
Ejemplo n.º 2
0
        public static GridItemCollection GetAllGridEntries(this PropertyGrid grid)
        {
            var view = grid.GetPropertyGridView();

            return((GridItemCollection)view.GetType().InvokeMember("GetAllGridEntries", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, view, null));
        }