Expand C# propertygrid on show

后端 未结 4 2192
星月不相逢
星月不相逢 2020-12-18 05:07

i have a question about property grid. when the form is shown i would like a group to be expand rather then collapsed. i have search a lot for that on the web and could not

4条回答
  •  天命终不由人
    2020-12-18 05:39

    These enumerator extensions allowed me to do everything I wanted.

    public static class PropertyGridExtensions
    {
        public static IEnumerable EnumerateGroups(this PropertyGrid propertyGrid)
        {
            if (propertyGrid.SelectedGridItem == null)
                yield break;
    
            foreach (var i in propertyGrid.EnumerateItems())
            {
                if (i.Expandable)
                {
                    yield return i;
                }
            }
        }
    
        public static IEnumerable EnumerateItems(this PropertyGrid propertyGrid)
        {
            if (propertyGrid.SelectedGridItem == null)
                yield break;
    
            var root = propertyGrid.SelectedGridItem;
            while (root.Parent != null)
                root = root.Parent;
    
            yield return root;
    
            foreach (var i in root.EnumerateItems())
            {
                yield return i;
            }            
        }
    
        public static GridItem GetGroup(this PropertyGrid propertyGrid, string label)
        {
            if (propertyGrid.SelectedGridItem == null)
                return null;
    
            foreach (var i in propertyGrid.EnumerateItems())
            {
                if (i.Expandable && i.Label == label)
                {
                    return i;
                }
            }
    
            return null;
        }
    
        private static IEnumerable EnumerateItems(this GridItem item)
        {
            foreach (GridItem i in item.GridItems)
            {
                yield return i;
    
                foreach (var j in i.EnumerateItems())
                {
                    yield return j;
                }
            }
        }        
    }
    

提交回复
热议问题