locked
Class attributes and PropertyGrid binding issues across a tabbed application RRS feed

  • Question

  • I have a tabbed application, and within each tab consists a PropertyGrid control. The PropertyGrid's selected object is a single class that accounts for all possible properties. The catch is, depending on the type of product assigned to the tab, some properties are not browsable at all. The class defaults each of these variable property's Browsable attribute to false, and the code enables the appropriate property at run-time. 

    Believe it or not, this works perfectly for the first tab to be opened in the application, regardless of the type of product assigned to the tab. The problem is, setting the Browsable attribute with more than one tab opened, affects all opened tabs.

    Is there any way to do this on just the single instance of the class and not all instances of the class? If it cannot be done at the class level, is there any way to extend the PropertyGrid control to hide properties programmatically regardless of the attribute from the class?

    Below is the logic I am working with to set the attribute at run-time. Please let me know if you guys have any ideas.

    private void SetPropertyBrowsable(string PropertyName, bool Browsable)
    {
        try
        {
            PropertyDescriptor _PropertyDescriptor = TypeDescriptor.GetProperties(this.GetType())[PropertyName];
    
            if (_PropertyDescriptor != null)
            {
                BrowsableAttribute _BrowsableAttribute = (BrowsableAttribute)_PropertyDescriptor.Attributes[typeof(BrowsableAttribute)];
                FieldInfo _IsBrowsable = _BrowsableAttribute.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
                _IsBrowsable.SetValue(_BrowsableAttribute, Browsable);
            }
        }
        catch (Exception) { /* DO NOTHING */ }
    }

    Sunday, April 12, 2020 2:30 PM

Answers

  • Karen Payne,

    I appreciate your response. I actually already stumbled across that CodeProject article and didn't consider it a viable option. Since you offered it up I decided to look at it again. Unfortunately, since I already have a pretty extensive class layout, I think I am too deep into the weeds to attempt this.

    Since I am using a database to determine what properties should be seen, I already have this in a generic list passed to my class. So what I eventually did, was created a method that refreshes the state of the class, setting every variable property Browsable to false that is not in this list. I then call it on the "tab index changed" event. This may be overkill, but it allows me to keep the logic I have already written and it works. Especially since the user can only have one tab opened at a time.

    Thanks again for your help.

    • Marked as answer by mkruluts Sunday, April 12, 2020 3:53 PM
    Sunday, April 12, 2020 3:53 PM

All replies

  • The following may do the trick for you, Bending the .NET PropertyGrid to Your Will.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, April 12, 2020 3:01 PM
  • Karen Payne,

    I appreciate your response. I actually already stumbled across that CodeProject article and didn't consider it a viable option. Since you offered it up I decided to look at it again. Unfortunately, since I already have a pretty extensive class layout, I think I am too deep into the weeds to attempt this.

    Since I am using a database to determine what properties should be seen, I already have this in a generic list passed to my class. So what I eventually did, was created a method that refreshes the state of the class, setting every variable property Browsable to false that is not in this list. I then call it on the "tab index changed" event. This may be overkill, but it allows me to keep the logic I have already written and it works. Especially since the user can only have one tab opened at a time.

    Thanks again for your help.

    • Marked as answer by mkruluts Sunday, April 12, 2020 3:53 PM
    Sunday, April 12, 2020 3:53 PM