设计器序列化和集合

时间:2010-05-12 09:44:20

标签: c# controls designer

我有一个代表网格的自定义控件;并实现另一个自定义控件。 在de designer中打开此控件时,我可以使用集合编辑器来设置我的集合。保存时;设计师成功保存了我的收藏品。

但是,在表单上删除此控件时;它仍然(并且应该)公开了集合属性,允许我修改我在另一个控件中定义的默认值。

然而;保存这个设计师时;它还尝试将预定义项存储在集合中;每次保存都会添加默认值。

解决此问题的最佳方法是什么?我附上了一个代码示例。

我定义了我的集合的代码示例: 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;
        }
    }

生成此网格的第一个“实现”的设计器代码; 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);

将此图片1放在另一张图片上时的代码示例; picture2.cs :(注意picture11是picture1,因为它是picture1的第1个;)

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);

picture2控件;当它重新生成InitializeComponent()方法时;现在添加我在picture1中添加的columndefinitions。

我已经为这个问题编写了这个临时修复程序:

{
    // 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;
}

1 个答案:

答案 0 :(得分:0)

我无法找到更好的方法。我决定坚持下面的解决方案。

{
    // 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;
}