views:

64

answers:

1

I've created a user control in a Windows Application C# 3.5 and it has a number of properties (string, int, color etc). These can be modified in the properties window and the values are persisted without problem.

However I've created a property like

  public class MyItem
  {
       public string Text { get; set; }
       public string Value { get; set; }
  }

  public class MyControl : UserControl
  {
       public List<MyItem> Items { get; set; }
  }

The properties dialog allows me to add and remove these items, but as soon as I close the dialog the values I entered are lost.

What am I missing? Many thanks!

+1  A: 

You need to initialise the the Items so an auto getter/setter won't help you here.

Try

public class MyControl : UserControl
{
    private List<MyItem> _items = new List<MyItem>();

    public List<MyItem> Items
    {
         get { return _items; }
         set { _items = value; }
    }
 }
Sophie88
You don't have to create a private variable in order to do this. He can continue to use the auto-property get; set notation, but then he'd need to initialize `Items = new List<MyItem>();` in a default constructor for the UserControl
Nick
There is a pretty "suspect" answer. It is very dubious that this would have *any* effect *at all*. Note to readers: YMMV - more than usual.
Marc Gravell