views:

29

answers:

0

Hey There,

I have an custom control that represents a grid; and implements another custom control. When opening this control in de designer, I am able to use the collection editor to set my collection. When saving; the designer successfully saves my collection.

However, when dropping this control on a form; it still (and should) expose(s) the collection property allowing me to modify the default values as i have defined in the other control.

However; when saving this designer; it also tries to store the predefined items in the collection; adding the default ones with every save.

What is the best way to solve this problem? I have attached a code sample.

Code sample where i have defined my collection: GridPicture.cs

    [Category("Layout")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Editor(typeof(CollectionEditor), typeof(UITypeEditor))]
    public GridPictureColumnDefinitionCollection ColumnDefinitions
    {
        // The DesignerSerializationVisibility attribute instructs the design editor to serialize the contents of the collection to source code.
        // This will place all the code required to add the items to a collection variable of GridPictureColumnDefinitionCollection.
        get
        {
            return m_ColumnDefinitions;
        }
    }

Generated designer code of my first 'implementation' of this grid; Picture1.cs

        VGTest.GridPictureColumnDefinition gridPictureColumnDefinition1 = new VGTest.GridPictureColumnDefinition();
        VGTest.GridPictureColumnDefinition gridPictureColumnDefinition2 = new VGTest.GridPictureColumnDefinition();
        VGTest.GridPictureRowDefinition gridPictureRowDefinition1 = new VGTest.GridPictureRowDefinition();
        gridPictureColumnDefinition1.Auto = true;
        gridPictureColumnDefinition1.Value = 0F;
        gridPictureColumnDefinition2.Auto = true;
        gridPictureColumnDefinition2.Value = 0F;
        this.ColumnDefinitions.Add(gridPictureColumnDefinition1);
        this.ColumnDefinitions.Add(gridPictureColumnDefinition2);
        gridPictureRowDefinition1.Auto = true;
        gridPictureRowDefinition1.Value = 0F;
        this.RowDefinitions.Add(gridPictureRowDefinition1);

Code sample when i place this picture1 on another picture; picture2.cs: (Note that picture11 is the picture1, as it is the 1st of picture1 ;)

VGTest.GridPictureColumnDefinition gridPictureColumnDefinition1 = new VGTest.GridPictureColumnDefinition();
            VGTest.GridPictureColumnDefinition gridPictureColumnDefinition2 = new VGTest.GridPictureColumnDefinition();
            VGTest.GridPictureColumnDefinition gridPictureColumnDefinition3 = new VGTest.GridPictureColumnDefinition();
            VGTest.GridPictureRowDefinition gridPictureRowDefinition1 = new VGTest.GridPictureRowDefinition(); 
// Some code removed that does the Auto and Value settings as above
            this.picture11.ColumnDefinitions.Add(gridPictureColumnDefinition1);
            this.picture11.ColumnDefinitions.Add(gridPictureColumnDefinition2);
            this.picture11.ColumnDefinitions.Add(gridPictureColumnDefinition3);

The picture2 control; when it regenerates the InitializeComponent() method; now adds the columndefinitions which i have added in picture1.

I have written this temporary fix for the problem:

{
    // This makes sure column definitions are only serialized when configured at a implementation of this GridPicture.
    // This is a Quick/Dirty fix for the following problem:
    // When MyPanel (:GridPicture) is put on PanelContainer(:Picture); the picture designer (re)serializes this each save.
    return this.GetType().BaseType.Name == typeof(Picture).Name;
}