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

Show and hide items

4 Answers 641 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Andrea
Top achievements
Rank 1
Andrea asked on 19 Feb 2019, 04:24 PM

Hello,

I'm trying to show and hide groups and single items of a PropertyGrid depending on the answers inside the same propertyGrid.

I've seen that a good place to set the visibility of the group is in when the grid is created, in this function:

private void RadPropertyGridOnCreateItemElement(object sender, CreatePropertyGridItemElementEventArgs e)
        {
            if (e.Item is PropertyGridGroupItem groupItem)
            {
                var myItem = (MyItem)groupItem.Group.GroupItem.GridItems[0].Value;
                   groupItem.Visible = myItem.EnableGroup;
            }
            if (e.Item is PropertyGridItem item)
            {
                var myItem = (MyItem)item.Value;
                item.Visible = myItem.EnableItem;
            }
        }

 

However, I'm not able to change the visibility at a second time, since this function is not called again later.

If I try to change the visibility of the VisualItem in OnItemFormatting, the items are hidden, but the spaces are not recalculated and the design is messed up.

Also, if I try to change the visibility of an item at the end of OnEdited, OnItemFormatting goes on loop.

 

Any hint? :)

Thanks!

4 Answers, 1 is accepted

Sort by
0
Andrea
Top achievements
Rank 1
answered on 19 Feb 2019, 04:33 PM

I also thought about recreating the store every time.

This could work, but also in this case, I can't do that on OnEdited event and I can't find another good event for that.

 

0
Accepted
Hristo
Telerik team
answered on 20 Feb 2019, 08:29 AM
Hi Valerio,

It appears that the RadPropertyGridOnCreateItemElement method in your code snippet is the handler of the RadPropertyGrid.CreateItemElement  event. This event will fire only once while initializing the control and it is suitable the initially hide or show certain groups and items. However, it cannot be used for any subsequent updates. Setting the Visible property in the ItemFormatting event indeed will cause an error as this will force a recursive update in the layout.

I can suggest iterating the Groups and Items collections directly exposed by the control. Depending on your actual scenario, you will be able to set the Visible property. Please check my code snippet below: 
private void radButton1_Click(object sender, EventArgs e)
{
    // Hide all groups
    foreach (object item in this.radPropertyGrid1.Groups)
    {
        PropertyGridGroupItem groupItem = item as PropertyGridGroupItem;
        if (groupItem != null)
        {
            groupItem.Visible = false;
        }
    }
}
 
private void radButton2_Click(object sender, EventArgs e)
{
    // Hide all items
    foreach (PropertyGridItem item in this.radPropertyGrid1.Items)
    {
        item.Visible = false;
    }
}

I hope this will help. Let me know if you need further assistance.

Regards,
Hristo
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Andrea
Top achievements
Rank 1
answered on 20 Feb 2019, 08:45 AM

Yes!

I also added RadPropertyGrid.BeginInit() and RadPropertyGrid.EndInit() and it works!

Thanks.

0
Hristo
Telerik team
answered on 20 Feb 2019, 09:53 AM
Hello Valerio,

I am glad that I managed to help. Begin/EndInit methods are part of the ISupportInitialize interface and these calls are usually done in the designer. Calling the EndInit at run-time will reset the binding context of the property grid.

Let me know if you have other questions.

Regards,
Hristo
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
PropertyGrid
Asked by
Andrea
Top achievements
Rank 1
Answers by
Andrea
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or