Show and hide items

5 posts, 1 answers
  1. VaL
    VaL avatar
    27 posts
    Member since:
    Oct 2017

    Posted 19 Feb Link to this post

    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!

  2. VaL
    VaL avatar
    27 posts
    Member since:
    Oct 2017

    Posted 19 Feb in reply to VaL Link to this post

    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.

     

  3. Answer
    Hristo
    Admin
    Hristo avatar
    1507 posts

    Posted 20 Feb Link to this post

    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.
  4. VaL
    VaL avatar
    27 posts
    Member since:
    Oct 2017

    Posted 20 Feb Link to this post

    Yes!

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

    Thanks.

  5. Hristo
    Admin
    Hristo avatar
    1507 posts

    Posted 20 Feb Link to this post

    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.
Back to Top