Property Grid - Browsable Attributes
-
:confused:hi i have a program where I want to change the value of BrowsableAttributes.The problem is like this: I have a property called Simulation which can have values either On or Off.If it is On i want to show property called OnText along with other properties and hide OffText property and if is Off i want to hide OnText and show OffText along with the common properties. For this i feel i have to change the value of BrowsableAttributes.but it doesn't allow me to do so as it is readonly. Kindly suggest me a way to get around this problem. thanks Tulika Shrivastava
-
:confused:hi i have a program where I want to change the value of BrowsableAttributes.The problem is like this: I have a property called Simulation which can have values either On or Off.If it is On i want to show property called OnText along with other properties and hide OffText property and if is Off i want to hide OnText and show OffText along with the common properties. For this i feel i have to change the value of BrowsableAttributes.but it doesn't allow me to do so as it is readonly. Kindly suggest me a way to get around this problem. thanks Tulika Shrivastava
hi! :) try to implement the ICustomTypeDescriptor interface on your control. then in the GetProperties overloaded methods, iterate all the properties then set the new attributes you want depending on your condition. here's an example:
//custom method to prevent redundant statement private PropertyDescriptorCollection x_HideShowProperties(PropertyDescriptorCollection baseProps) { PropertyDescriptor[] props = null; for (int i = 0; i < baseProps.Count; i++) { if (/*condition here*/) { if (props == null) { props = new PropertyDescriptor[baseProps.Count]; baseProps.CopyTo(props, 0); } props[i] = TypeDescriptor.CreateProperty( this.GetType(), baseProps[i], new BrowsableAttribute(false), new DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)); } } if (props == null) { return baseProps; } else { return new PropertyDescriptorCollection(props); } } PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() { return x_HideShowProperties(TypeDescriptor.GetProperties(this, true)); } public PropertyDescriptorCollection GetProperties(Attribute[] attributes) { return x_HideShowProperties(TypeDescriptor.GetProperties(this, attributes, true)); }
then on your property(Simulation), add the attribute "RefreshProperties(RefreshProperties.All)". this will refresh the propertygrid. if your not yet familiar on this interface, just try to research it on the msdn or other .net help sites. hope that helps. :) microsoc -
hi! :) try to implement the ICustomTypeDescriptor interface on your control. then in the GetProperties overloaded methods, iterate all the properties then set the new attributes you want depending on your condition. here's an example:
//custom method to prevent redundant statement private PropertyDescriptorCollection x_HideShowProperties(PropertyDescriptorCollection baseProps) { PropertyDescriptor[] props = null; for (int i = 0; i < baseProps.Count; i++) { if (/*condition here*/) { if (props == null) { props = new PropertyDescriptor[baseProps.Count]; baseProps.CopyTo(props, 0); } props[i] = TypeDescriptor.CreateProperty( this.GetType(), baseProps[i], new BrowsableAttribute(false), new DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)); } } if (props == null) { return baseProps; } else { return new PropertyDescriptorCollection(props); } } PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() { return x_HideShowProperties(TypeDescriptor.GetProperties(this, true)); } public PropertyDescriptorCollection GetProperties(Attribute[] attributes) { return x_HideShowProperties(TypeDescriptor.GetProperties(this, attributes, true)); }
then on your property(Simulation), add the attribute "RefreshProperties(RefreshProperties.All)". this will refresh the propertygrid. if your not yet familiar on this interface, just try to research it on the msdn or other .net help sites. hope that helps. :) microsochi:) thanx a lot it solved my problem
-
hi:) thanx a lot it solved my problem