I have one class, say AcmeWidget that contains a complex property call it
legend defined like so:
public class AcmeWidget : Control {
...
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Legend Legend {
get { return m_legend; }
}
}
[TypeConverter(ExpandableObjectConverter)]
public class Legend {
...
public Color Color {
get { ... }
set {
if (value == m_color) return;
m_color = value;
m_parent.Invalidate;
}
}
private bool ShouldSerializeColor() {
return (m_color != c_defaultColor);
}
private void ResetColor() {
this.Color = c_defaultColor;
}
}
This seems to work fine and dandy until I use the "Reset" context menu
item on the nested property "AcmeWidget.Legend.Color" in the PropertyGrid. This isn't
enough to trigger code serialization so the previous value stays in the
InitializeComponent method. If I Reset the color and then tweak something else
that forces a code serialization then I get the proper "reset" color serialized out to
InitializeComponent. What do I do to force the designer call to
ResetColor() to cause the internal dirty flag to be set that indicates
that code serialization is required.
After some debugging it appears that the code is doing what I told it. I
hit the Reset in the property grid, the ResetColor method gets called.
Now m_color is set equal to c_defaultColor. Then the designer calls my
ShouldSerializeColor method and of course the two are equal and it tells
the designer "oh no, you don't need to serialize this property". That
just seems broken. If I have "Reset" a color that should set a dirty flag
somewhere in the designer that says code serialization needs to be done.
I'm still left wondering how to "tickle" the designer to get it to code
serialize on a Reset* method.
Finally, why doesn't this work?
[TypeConverter(ExpandableObjectConverter)]
public class Legend {
...
[DefaultValue(typeof(Color), "Color.Black")]
public Color Color {
get { ... }
set {
if (value == m_color) return;
m_color = value;
m_parent.Invalidate;
}
}
}
Then I wouldn't need the ShouldSerializeColor or the ResetColor methods.
--
Keith
Andrew S \(Infragistics\) - 16 Jun 2004 23:46 GMT
You need to either get the IComponentChangeService and notify it of the changes (OnComponentChanging/ed) or instead of changing the property directly, you need to get the propertydescriptor for that property (see TypeDescriptor.GetProperties) and change the property value using that method.
I have one class, say AcmeWidget that contains a complex property call it
legend defined like so:
public class AcmeWidget : Control {
...
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Legend Legend {
get { return m_legend; }
}
}
[TypeConverter(ExpandableObjectConverter)]
public class Legend {
...
public Color Color {
get { ... }
set {
if (value == m_color) return;
m_color = value;
m_parent.Invalidate;
}
}
private bool ShouldSerializeColor() {
return (m_color != c_defaultColor);
}
private void ResetColor() {
this.Color = c_defaultColor;
}
}
This seems to work fine and dandy until I use the "Reset" context menu
item on the nested property "AcmeWidget.Legend.Color" in the PropertyGrid. This isn't
enough to trigger code serialization so the previous value stays in the
InitializeComponent method. If I Reset the color and then tweak something else
that forces a code serialization then I get the proper "reset" color serialized out to
InitializeComponent. What do I do to force the designer call to
ResetColor() to cause the internal dirty flag to be set that indicates
that code serialization is required.
After some debugging it appears that the code is doing what I told it. I
hit the Reset in the property grid, the ResetColor method gets called.
Now m_color is set equal to c_defaultColor. Then the designer calls my
ShouldSerializeColor method and of course the two are equal and it tells
the designer "oh no, you don't need to serialize this property". That
just seems broken. If I have "Reset" a color that should set a dirty flag
somewhere in the designer that says code serialization needs to be done.
I'm still left wondering how to "tickle" the designer to get it to code
serialize on a Reset* method.
Finally, why doesn't this work?
[TypeConverter(ExpandableObjectConverter)]
public class Legend {
...
[DefaultValue(typeof(Color), "Color.Black")]
public Color Color {
get { ... }
set {
if (value == m_color) return;
m_color = value;
m_parent.Invalidate;
}
}
}
Then I wouldn't need the ShouldSerializeColor or the ResetColor methods.
--
Keith
Eric Cadwell - 17 Jun 2004 15:40 GMT
Read the section on "Playing nicely with others"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/c
ustdsgnrdotnet.asp
HTH;
Eric Cadwell
http://www.origincontrols.com