C# 关于开发控件,如何自定义属性归类

首先介绍下属性分类结构:

 

绿色框:是总分类。

红色框:是子分类。

(当然子分类下面还可以包含子分类,跟Node节点一回事。)

灰色框:是子分类下面的属性.

 

Q:如何将自己写的属性归类到指定的总分类

A:使用Category属性,例子如下:

 [Description("The image associated with the control"),Category("Appearance")] 
 public Image MyImage {
    get {
       // Insert code here.
       return image1;
    }
    set {
       // Insert code here.
    }
 }

 

Q:如何自定义一个子分类

A:以创建一个项目作为答案吧,具体步骤如下:

首先看下项目结构图:

  • 1)创建一个C#程序,添加一个UserControl和WinForm。
  • 2)UserControl中代码如下:
  • 2.1)先创建一个类文件MyAttribute,定义复合属性:
namespace WinFormCSharp
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class MyAttribute
    {
        [NotifyParentProperty(true)]
        public int Id { get; set; }
        [NotifyParentProperty(true)]
        public string Name { get; set; }
    }
}

  

【然后在UserControl中引用】

public partial class UserControl1 : UserControl
    {
        private MyAttribute _attr=null;

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public MyAttribute MyAttribute 
        {
            get
            {
                if (_attr == null)
                {
                    _attr = new MyAttribute();
                }
                return _attr;
            }
        }

        public UserControl1()
        {
            InitializeComponent();
        }

    }

最后编译项目,从工具条中拖拽这个控件到WinForm上即可。

参考:http://www.wxzzz.com/?id=104

 

转载于:https://www.cnblogs.com/andrew-blog/archive/2012/06/18/ControlGroup.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值