开发者社区> 橘子红了呐> 正文

在C#中使用属性控件添加属性窗口

简介:
+关注继续查看

转自原文 在C#中使用属性控件添加属性窗口

第一步,创建在应用程序中将要展现的字段属性为public公有属性。其中,所有的属性必须有get和set的方法(如果不设置get方法,则要显示的属性不会显示在属性控件中)。为了设置相关的属性,必须设置下面的一些关于属性控件的属性值,如下表所示:


属性值 含义
CategoryAttribute 该属性对在Property控件中的属性按字母顺序进行归类
DescriptionAttribute 其值为对每个属性的具体文字描述,将会显示在property控件的底部
BrowsableAttribute 该值为是否在property控件中显示或者隐藏某个属性
ReadOnlyAttribute 该值为某个属性值是否在property控件中只读
DefaultValueAttribute 每个属性的默认值


接下来,我们创建一个用户类,并且使用属性控件,使得可以在属性控件框中改变其值。我们先引入相关的命名空间:

using System.ComponentModel;


之后,创建相关的类,设置有关的属性,代码如下:

/// Customer class to be displayed in the property grid

/// </summary>

/// [DefaultPropertyAttribute("Name")]


public class Customer
{
private string _name;
private int _age; 
private DateTime _dateOfBirth; 
private string _SSN; 
private string _address; 
private string _email; 
private bool _frequentBuyer; 
[CategoryAttribute("ID Settings"), DescriptionAttribute("Name of the customer")]   

public string Name 
{
get 
{
return _name; 
}
set
{
_name = value;
}
}
[CategoryAttribute("ID Settings"), DescriptionAttribute("Social Security Number of the customer")] 

public string SSN
{
get
{
return _SSN;
}
set
{
_SSN = value;
}
}
[CategoryAttribute("ID Settings"), DescriptionAttribute("Address of the customer")] 
public string Address 
{
get
{
return _address;
}
set
{
_address = value;
}
}
[CategoryAttribute("ID Settings"), DescriptionAttribute("Date of Birth of the Customer (optional)")]
public DateTime DateOfBirth 
{
get { return _dateOfBirth; }
set { _dateOfBirth = value; }
}
[CategoryAttribute("ID Settings"), DescriptionAttribute("Age of the customer")] 
public int Age 

get { return _age; } 
set { _age = value; } 
}
[CategoryAttribute("Marketting Settings"), DescriptionAttribute("If the customer has bought more than 10 times, this is set to true")]
public bool FrequentBuyer
{
get { return _frequentBuyer; }
set { _frequentBuyer = value; }
}
[CategoryAttribute("Marketting Settings"), DescriptionAttribute("Most current e-mail of the customer")]
public string Email 
{
get { return _email; }
set { _email = value; } 
}
public Customer() { }
}


可以看到,在上面的代码中,我们对customer类中的属性进行了设置,如姓名,出生日期,地址等。
接着,我们要为创建的customer类创建一个实例,并且将其与属性控件绑定。属性控件会自动根据类中对属性的相关设置,从而在界面中显示有关的属性,并且还可以进行编辑,比如,可以对生日属性进行修改,修改时会弹出日历控件框,十分方便。代码如下:

private void Form1_Load(object sender, System.EventArgs e)
{
//创建bill对象,实例化CUSTOMER类 
Customer bill = new Customer();
//赋值给属性
bill.Age = 50;
bill.Address = " 114 Maple Drive ";
bill.DateOfBirth = Convert.ToDateTime(" 9/14/78");
bill.SSN = "123-345-3566";
bill.Email = “bill@aol.com”
bill.Name = "Bill Smith"; 
//将对象绑定到property控件中
propertyGrid1.SelectedObject = bill;
}


最后,运行程序,我们就得到了本文一开始图示的结果了。再来回顾下该程序,其中我们使用了CatrgoryAttribute属性,定义了id settings和MarketSettings,它们在属性控件中以分类的形式出现(注意它们前有个“+”号,点击可以展开看到其子属性)。同时,我们每当选择一个属性时,在属性控件框的下部,会同时显示该属性的相关描述。

Property属性控件还有很多优点,本文只是对其做了简单介绍,希望能给读者启发。

 

没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。



    本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/6265482.html,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
关于Navisworks属性查看的问题(属性不显示)
关于Navisworks属性查看的问题(属性不显示)
16 0
Qt动态添加控件并设置大小位置等属性
Qt动态添加控件并设置大小位置等属性
259 0
窗体间动态传值
窗体间动态传值
40 0
使用 JavaScript 中的 document 对象的属性,根据下拉框中选择的属性,更改页面中的字体颜色和背景颜色
使用 JavaScript 中的 document 对象的属性,根据下拉框中选择的属性,更改页面中的字体颜色和背景颜色
367 0
WPF UserControl 的绑定事件、属性、附加属性
原文:WPF UserControl 的绑定事件、属性、附加属性 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Vblegend_2013/article/details/83477473 ...
1818 0
WPF属性(二)附加属性
原文:WPF属性(二)附加属性 附加属性是说一个属性本来不属于某个对象,但由于某种需求而被后来附加上,也就是把对象放入一个特定环境后对象才具有的属性就称为附加属性,附加属性的作用就是将属性与数据类型解耦,让数据类型的...
710 0
+关注
橘子红了呐
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载