Is there a way to get EditorAttribute to use the property set method?

T

tribbles

I am using the PropertyGrid to handle management of the various
configurable properties on my class. I have noticed however, that when
you assign an editor to a given property that it does not use the set
method.

With the property I included below, I set a break point on the 'set'
method. During debug that line never gets executed. Near as I can
tell, reflection is used to set data directly on the private variable
'control.item'. A break point on the 'set' method of the
'control.Items' property verifies that it never gets called. The
setVisual function takes the names of all the items in the collection
and adds them to the tree view so they show up in the designer at
design time. Serialization works and when I deserialize the object the
names show up fine. When I use a context method to refresh, the items
will show up as well.

Anyway, seems sloppy that the editors don't call the set methods! Can
you force a given editor to respect the setters? If the answer is
no... Anyone know of any possible workarounds? My current idea is to
code a custom editor and having it post a changed event that I add
during the gettor to cause the needed refresh logic. There has got to
be a better way...

//
//<Category> Items
//
[XSerialize(5)]
[XmlNode("item-list")]
//[DefaultValue("")] // Has to be defaulted in the
constructor.
[Category("Items")]
[Description("The name value items")]
[Editor(typeof(CollectionEditor),
typeof(System.Drawing.Design.UITypeEditor))]
public NameValueObjectCollection Items
{
get { return control.Items; }
set { control.Items = value; SetVisual()}
}
 
S

Stoitcho Goutsev \(100\)

tribbles,

Your property is a collection. When you add,remove or edit elements in the
collection the editor doesn't create a new collection. It uses the original
one and just add or remove elements from there. This way it expectable that
your setter method is not called. When you dig deeply in the collection
editor you'll fine more problems such as the editor works with its own copy
of the collection and when you press the OK button it clears your entire
collection and re-populates it. It all depends on what operations you are
performed on the collection. It is sometimes hard to discover which are the
new elements which are not. In other words working with that editor can be
really frustrating.

Back to the question you asked. The Property Grid uses set accessors to set
properties. It uses reflection, but only to get the setter. It never
modifies directly the backupfield (there is no way it can find it). It also
considers only public members.
 

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