Setting properties of a property in the Property Grid

N

Nathan Sokalski

I have a custom control with a property that is an instance of a class I
wrote for my control. The Property Grid only shows the property name, but
you cannot set the properties. How can I make the Property Grid let users
set the properties of the property? Thanks.
 
J

James Hahn

Your property will not appear in the designer without extra work. You will
need to create a custom editor, or implement in-place editing. This is the
code that enables the user to enter values at design time that are converted
into your type. It's not as complex as it sounds - you can pretty much just
follow the templates:

http://msdn.microsoft.com/en-us/library/bb907382.aspx

You will also need to implement custom converters (to and from string) so
that your object can be persisted in user settings and so that the designer
can serialize the object and remember the current property value at design
time. Again, you can pretty much just follow the templates:

http://msdn.microsoft.com/en-us/library/ayybcxe5.aspx
 
N

Nathan Sokalski

Well, I've made it to the point where I can make it do one of two things:

1. I can make it expand to show the properties, but it doesn't add anything
to the *.aspx file, so bothering to set the properties is kind of pointless.

2. I can make it automatically display all the properties in the Property
Grid as ObjectName-PropertyName, which allows me to edit them as if they
were individual properties. However, I would like the properties to be
expandable. Here is the basic code I currently have which makes it do what I
just mentioned:


Public Class MyClass
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public Property MyProperty() As MyType
End Property
End Class

<TypeConverter(GetType(System.ComponentModel.ExpandableObjectConverter)),
Serializable()> Public Class MyType
<RefreshProperties(RefreshProperties.All), NotifyParentProperty(True)>
Public Property Prop1 as String
<RefreshProperties(RefreshProperties.All), NotifyParentProperty(True)>
Public Property Prop2 as String
<RefreshProperties(RefreshProperties.All), NotifyParentProperty(True)>
Public Property Prop3 as String
<RefreshProperties(RefreshProperties.All), NotifyParentProperty(True)>
Public Property Prop4 as String
End Class


This causes the Property Grid to show:

MyProperty-Prop1
MyProperty-Prop2
MyProperty-Prop3
MyProperty-Prop4

I am looking to have it show something like:

+MyProperty
Prop1
Prop2
Prop3
Prop4

Both ways should use attributes of the format ObjectName-PropertyName to add
the values to the control in the *.aspx file, but I want Visual Studio
should display it in the Property Grid using the expandable format. Can
anybody help me here? Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top