C#自定义控件的集合属性怎么搞?求指点

chuchur 2013-03-29 04:40:19
如题, 我要写一个自定义控件 类似于Listbox,但是我又不想继承Listbox,其他都没有问题,在自定义集合属性那里卡住了,求高手指点迷津。。不胜感激!

默认的Listbox 只能添加 只能添加一个值,我需要添加2个字符串,一个显示在左边,一个显示在右边,类似于千千静听的播放列表一样,左边显示歌曲名,右边显示时长。

写了一半就是不知道集合属性该怎么弄。
...全文
789 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuchur 2013-03-29
  • 打赏
  • 举报
回复
引用 11 楼 gxingmin 的回复:
呵呵,这样就可以保存,亲测 C# code?12345678910111213141516171819 public partial class myListBox : Control { private List<BsItem> items = new List<BsItem>(); [TypeConverter(t……
是的。我多写了一个set{}...其实我知道是多余的,只是有缓存。。刷了好几次。。纠结了很久。。刚清理下然后重新生成就OK了。 多些你们这些热心的人。。致敬!!
gxingmin 2013-03-29
  • 打赏
  • 举报
回复
呵呵,这样就可以保存,亲测
 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; } 
    }
chuchur 2013-03-29
  • 打赏
  • 举报
回复
搞定了,谢谢个位。。。

 private List<ListItem> items = new List<ListItem>();
        public List<ListItem> Items
        {
            get
            {
                return items;
            }
            //set { if (items == null)items = new List<ListItem>(); items = value; }
        }
chuchur 2013-03-29
  • 打赏
  • 举报
回复
但是不好使啊。。属性窗口里能能添加数值,储存不了。。
chuchur 2013-03-29
  • 打赏
  • 举报
回复
引用 7 楼 gxingmin 的回复:
这样中么? C# code?1234567891011121314 public class myListBox : Control { [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [Localizable(true)] ……
我是这么干的 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; } } }
gxingmin 2013-03-29
  • 打赏
  • 举报
回复
这样中么?
 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; }
    }
chuchur 2013-03-29
  • 打赏
  • 举报
回复
引用 5 楼 tanghuawei 的回复:
你可以重写ListBox属性,然后[Browsable(false)]就行,然后可以加入自己的自定义属性
具体该怎么写呢。谢谢
tanghuawei 2013-03-29
  • 打赏
  • 举报
回复
你可以重写ListBox属性,然后[Browsable(false)]就行,然后可以加入自己的自定义属性
Echo001 2013-03-29
  • 打赏
  • 举报
回复
设置一个只读 Get 属性的集合.
chuchur 2013-03-29
  • 打赏
  • 举报
回复
引用 2 楼 gxingmin 的回复:
你是不是想这个集合,在设计界面时,在属性窗口里能添加这个集合?
是的,我就这样想的,具体怎么做呢,求指点
gxingmin 2013-03-29
  • 打赏
  • 举报
回复
你是不是想这个集合,在设计界面时,在属性窗口里能添加这个集合?
chuchur 2013-03-29
  • 打赏
  • 举报
回复
百度谷歌蛮长时间,都没有这样的例子。。顶起。。。。

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧