如题, 我要写一个自定义控件 类似于Listbox,但是我又不想继承Listbox,其他都没有问题,在自定义集合属性那里卡住了,求高手指点迷津。。不胜感激!默认的Listbox 只能添加 只能添加一个值,我需要添加2个字符串,一个显示在左边,一个显示在右边,类似于千千静听的播放列表一样,左边显示歌曲名,右边显示时长。写了一半就是不知道集合属性该怎么弄。自定义控件c#集合属性

解决方案 »

  1.   

    设置一个只读 Get  属性的集合.
      

  2.   

    你可以重写ListBox属性,然后[Browsable(false)]就行,然后可以加入自己的自定义属性
      

  3.   

    这样中么?
     public class myListBox : Control
        {
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            [Localizable(true)]
            [MergableProperty(false)]
            public List<myItem> Items { get; set; }
        }    [Serializable()]
        public class myItem
        {
            public string LeftString { get; set; }
            public string RightString { get; set; }
        }
      

  4.   


    我是这么干的
     private List<ListItem> items;
            public List<ListItem> Items
            {
                get
                {
                    return items;
                }
                set { if (items == null)items = new List<ListItem>(); items = value; }
            }
    。。
    [Serializable()]
        public class ListItem
        {
            public ListItem() { }        public ListItem(string songname, int songlenght)
            {
                this.songname = songname;
                this.songlenght = songlenght;
            }
            private string songname;
            private int songlenght;
    public string SongName { get { return songname; } set { songname = value; } }
            public int SongLenght { get { return songlenght; } set { songlenght = value; } }
        }
      

  5.   

    搞定了,谢谢个位 private List<ListItem> items = new List<ListItem>();
            public List<ListItem> Items
            {
                get
                {
                    return items;
                }
                //set { if (items == null)items = new List<ListItem>(); items = value; }
            }
      

  6.   

    呵呵,这样就可以保存,亲测
     public partial class myListBox : Control 
        {
            private List<BsItem> items = new List<BsItem>();        
            [TypeConverter(typeof(System.ComponentModel.CollectionConverter))]       
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            public List<BsItem> Items 
            { 
                get 
                { return items; }
            }
            public myListBox() {  }
        }
        public class BsItem
        { 
            public string name 
            { get; set; } 
            public string desc 
            { get; set; } 
        }
      

  7.   


    是的。我多写了一个set{}...其实我知道是多余的,只是有缓存。。刷了好几次。。纠结了很久。。刚清理下然后重新生成就OK了。多些你们这些热心的人。。致敬!!