This is a migrated thread and some comments may be shown as answers.

PropertyGrid: to collapse some groups of PropertyGrid programmatically

4 Answers 531 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Eusebio
Top achievements
Rank 1
Veteran
Eusebio asked on 30 Nov 2020, 01:16 PM

I collapse some groups of RadPropertyGrid programmatically, but it's don't work. Those groups collapse, but I can't expand it with the mouse. those appear as disabled.

 

        #region RPG: CONFIGURACION DE GRUPOS----------------------ini

        private void rpgMaquina_CustomGrouping(object sender, PropertyGridCustomGroupingEventArgs e)
        {
            switch (e.Item.Category)
            {
                case "General": e.GroupKey = 0; e.Handled = true; break;
                case "Inventario": e.GroupKey = 1; e.Handled = true; break;
           
                case "Pares de apriete": e.GroupKey = 2; e.Handled = true; break;
                case "Accesorios": e.GroupKey = 3; e.Handled = true; break;

                default:
                    break;
            }


        }

        private void rpgMaquina_ItemFormatting(object sender, PropertyGridItemFormattingEventArgs e)
        {
            PropertyGridGroupItem groupItem = e.Item as PropertyGridGroupItem;
            if (groupItem != null)
            {
                e.Item.Label = groupItem.GridItems[0].Category;
                if (e.Item.Label == "Inventario" || e.Item.Label == "Accesorios")
                {
                    groupItem.Collapse();
                    //groupItem.Enabled = true;

                }

            }
        }
        #endregion RPG: CONFIGURACION DE GRUPOS-------------------fin

4 Answers, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 30 Nov 2020, 03:42 PM

Hello, Eusebio,

Following the provided brief information it seems that you use custom grouping for the items in RadPropertyGrid and you collapse some of the PropertyGridGroupItems in the ItemFormatting event. Note, that once you collapse the PropertyGridGroupItem you shouldn't be able to expand the group. 

I created a sample project to demonstrate this. The result is demonstrated in the attached gif file.

 public RadForm1()
 {
     InitializeComponent();
     this.radPropertyGrid1.SelectedObject = new RadButton();
     radPropertyGrid1.ToolbarVisible = true;
     this.radPropertyGrid1.EnableCustomGrouping = true;
     this.radPropertyGrid1.CustomGrouping += new PropertyGridCustomGroupingEventHandler(radPropertyGrid1_CustomGrouping);
     radPropertyGrid1.ItemFormatting += new PropertyGridItemFormattingEventHandler(radPropertyGrid1_ItemFormatting);
     GroupDescriptor descriptor = new GroupDescriptor("PropertyType");
     this.radPropertyGrid1.GroupDescriptors.Add(descriptor);
 }
 void radPropertyGrid1_ItemFormatting(object sender, PropertyGridItemFormattingEventArgs e)
 {
     PropertyGridGroupItem groupItem = e.Item as PropertyGridGroupItem;
     if (groupItem != null)
     {
         e.Item.Label = groupItem.GridItems[0].Category;
         if (e.Item.Label == "Behavior")
         {
             groupItem.Collapse();
         }
     }
 }
 private void radPropertyGrid1_CustomGrouping(object sender, PropertyGridCustomGroupingEventArgs e)
 {
     Type propertyType = e.Item.PropertyType;
     if (propertyType.IsClass)
     {
         e.GroupKey = "Reference type";
     }
     else
     {
         e.GroupKey = "Value type";
     }
 }

Could you give the provided project a try and let me know how it works for you? What is the exact behavior that you are trying to obtain?

I am looking forward to your reply.

Regards,
Nadya
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Eusebio
Top achievements
Rank 1
Veteran
answered on 30 Nov 2020, 04:12 PM

Thanks Nadya, but you don't seem to have understood me. Your code is similar to mine.

We need is that by default when the form is displayed, some groups are collapsed and that they are enabled so that they can be expanded or collapsed.

 

Best regards.

0
Eusebio
Top achievements
Rank 1
Veteran
answered on 01 Dec 2020, 08:10 AM
Also, if we use the CollapseAllGridItems() method, it collapses all the groups and does allow collapse/expand with the mouse.

0
Nadya | Tech Support Engineer
Telerik team
answered on 01 Dec 2020, 09:29 AM

Hello, Eusebio,

Thank you for clarifying that you want to have some groups collapsed when the form is loaded and be able to expand the groups later. 

Note, ItemFormatting event is used for customizing the appearance of the items and it fires many times. In this case, when you want to be able to expand the groups later it is not suitable to collapse the groups in this event. However, Collapse and Expand methods are the right methods for collapsing. expanding items. CollapseAllGridItems method collapses all the categories in the PropertyGridElement.

In order to achieve your custom requirement, I can suggest using the ItemExpandedChanging event that fires before the value of the Expanded property of a property grid item is changed. This event allows being canceled. Thus, you can manage when to expand/collapse groups as per your requirement.

More useful properties, methods, and events you can find here: https://docs.telerik.com/devtools/winforms/controls/propertygrid/properties-methods-events 

I modified the provided project in order to demonstrate this approach:

 public RadForm1()
 {
     InitializeComponent();
     this.radPropertyGrid1.SelectedObject = new RadButton();
     radPropertyGrid1.ToolbarVisible = true;
     this.radPropertyGrid1.EnableCustomGrouping = true;
     this.radPropertyGrid1.CustomGrouping += new PropertyGridCustomGroupingEventHandler(radPropertyGrid1_CustomGrouping);
     radPropertyGrid1.ItemFormatting += new PropertyGridItemFormattingEventHandler(radPropertyGrid1_ItemFormatting);
     GroupDescriptor descriptor = new GroupDescriptor("PropertyType");
     this.radPropertyGrid1.GroupDescriptors.Add(descriptor);
     this.radPropertyGrid1.ItemExpandedChanging += this.RadPropertyGrid1_ItemExpandedChanging;

     this.radPropertyGrid1.PropertyGridElement.PropertyTableElement.Groups[0].Collapse();
 }

 private void RadPropertyGrid1_ItemExpandedChanging(object sender, RadPropertyGridCancelEventArgs e)
 {
     if (!canExpand && e.Item.Label == "Behavior")
     {
         e.Cancel = true;
     }
 }

 void radPropertyGrid1_ItemFormatting(object sender, PropertyGridItemFormattingEventArgs e)
 {
     PropertyGridGroupItem groupItem = e.Item as PropertyGridGroupItem;
     if (groupItem != null)
     {
         e.Item.Label = groupItem.GridItems[0].Category;
        
     }
 }
 private void radPropertyGrid1_CustomGrouping(object sender, PropertyGridCustomGroupingEventArgs e)
 {
     Type propertyType = e.Item.PropertyType;
     if (propertyType.IsClass)
     {
         e.GroupKey = "Reference type";
     }
     else
     {
         e.GroupKey = "Value type";
     }
    
 }
 
 bool canExpand=false;
 private void radButton1_Click(object sender, EventArgs e)
 {
     foreach (PropertyGridGroupItem groupItem in this.radPropertyGrid1.PropertyGridElement.PropertyTableElement.Groups)
     {
         if (groupItem.Label == "Behavior")
         {
             canExpand = true;
         }
     }
 }


I hope this helps. If you have other questions please let me know.

Regards,
Nadya
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
PropertyGrid
Asked by
Eusebio
Top achievements
Rank 1
Veteran
Answers by
Nadya | Tech Support Engineer
Telerik team
Eusebio
Top achievements
Rank 1
Veteran
Share this question
or