Search code examples
c#windows-mobilecompact-frameworkvisual-studio-designer

Compact framework custom control with collection not saving


I am creating a menu control for the compact framework. The control has a collection of items. I can add items using the visual studio designer, however, they are never added to the designer file and therefore next time I open the designer or build the solution the menu no longer has any items.

Here is the code for the Item:

public class Item
{
    public int ImageIndex { get; set; }

    private string _text = "";
    public string Text
    {
        get { return _text ?? ""; }
        set { _text = value ?? ""; }
    }

    public string Name { get; set; }
}

Here is the relevant code for the Menu:

public class ItemMenu : Control
{
    public ItemMenu()
    {
        Columns = 4;
        RowHeight = 64;
        ColumnWidth = 64;
        _items.ListChanged += new ListChangedEventHandler(_items_ListChanged);
    }

    private BindingList<Item> _items = new BindingList<Item>();
    public BindingList<Item> Items
    {
        get { return _items; }
    }

    public ImageList NormalImages { get; set; }
    public ImageList SelectedImages { get; set; }

    public int Columns { get; set; }
    public int RowHeight { get; set; }
    public int ColumnWidth { get; set; }
    public int Rows
    {
        get { return (int)Math.Ceiling((double)Items.Count / (double)Columns); }
    }

    private void _items_ListChanged(object sender, ListChangedEventArgs e)
    {
        Refresh();
    }
}

Does anyone know how I can get the collection of items to save to the designer file when I edit it in the visual studio designer?


Solution

  • I'll try to answer this the best I can while I'm away for the holidays, but you need to specify a few design time attributes for your control. There is a DesignTimeAttributes file that you can add to your Controls assembly. It is an XML file, I believe with the XMTA extension. In the XML file for each class you can specify different design time attributes, as well as link a design time editor to the control etc.

    The attribute you need to experiment with is the DesignerSerializationVisibility Attribute. I believe you can set this to Content so that it will correctly serialize your collection.

    If you need more direction, I can update my answer when I get back to where I have an example.