景林

爬上巨人的肩膀

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

零.引言

  PropertyGrid显示一个对象的属性和事件时,可以设置其默认属性和事件,也就是当你选中对象时,propertyGrid中焦点在哪一个属性或事件上。为对象的属性提供默认值,使PropertyGrid显示属性时,更加友好。

一.默认属性和默认事件

  PropertyGrid能识别默认属性和事件,例如在设计时,双击Form框,就会跳到Form的Load事件中,这是因为Form的默认事件是Load。当你选中属性框中的某一项后,该项会着色(蓝色)选中,在属性和事件选项卡之间切换,就会发现,选中的始终是默认的属性和事件(例如,Form,选中的就是Text属性和Load事件)。

  下面我们就来设计默认属性和事件,还是以MyControl为例。

  
 1     //控件
 2     [DefaultProperty("Angle")]
 3     [DefaultEvent("Test")]
 4     public class MyControl : System.Windows.Forms.UserControl
 5     {
 6         private double _angle = 90D;
 7         private Color _penColor = Color.Red;
 8 
 9         public delegate void TestDefaultEvent();
10         public event TestDefaultEvent Test;
11 
12         [BrowsableAttribute(true)]
13          public double Angle
14         {
15             get
16             { return _angle; }
17             set
18             { _angle = value; }
19         }
20 
21         [Browsable(true)]
22         public Color PenColor
23         {
24             get
25             {
26                 return _penColor;
27             }
28             set
29             {
30                 _penColor = value;
31                 Invalidate();
32             }
33         }
34         public MyControl()
35         {
36             
37         }
38 
39         protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
40         {
41             e.Graphics.DrawString("The Angle is " + _angle, this.Font, new SolidBrush(this._penColor),0,0);
42         }
43 }
View Code

  非常简单,只需要给类加上DefaultProperty和DefaultEvent特性,在特性中指明属性或事件名称即可。

二.属性的默认值

  一般来说,属性都有一个默认值。我们经常看到,在PropertyGrid中,有些属性字体是没有加粗的,而有些却加粗,没加粗的是默认属性,修改后就会变粗,改为默认值后,又不加粗,这是为了方便开发人员设别哪些属性修改了的。

  如何达到这样的效果,需要给属性添加默认值。这里又分两种情况,对于基础类型(如int,float等)直接加上默认值特性即可,但对于一些复杂的属性,则要使用函数来设置,如下的例子:

  
 1     //控件
 2     [DefaultProperty("Angle")]
 3     [DefaultEvent("Test")]
 4     public class MyControl : System.Windows.Forms.UserControl
 5     {
 6         private double _angle = 90D;
 7         private Color _penColor = Color.Red;
 8 
 9         public delegate void TestDefaultEvent();
10 
11         public event TestDefaultEvent Test;
12 
13         [BrowsableAttribute(true)]
14         [DefaultValue(90D)]
15         public double Angle
16         {
17             get
18             { return _angle; }
19             set
20             { _angle = value; }
21         }
22 
23         [Browsable(true)]
24         public Color PenColor
25         {
26             get
27             {
28                 return _penColor;
29             }
30             set
31             {
32                 _penColor = value;
33                 Invalidate();
34             }
35         }
36 
37         public void ResetPenColor()
38         {
39             PenColor = Color.Red;
40         }
41 
42         public bool ShouldSerializePenColor()
43         {
44             return PenColor != Color.Red;
45         }
46 
47 
48         public MyControl()
49         {
50             //this._angle = 90;
51         }
52 
53         protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
54         {
55             e.Graphics.DrawString("The Angle is " + _angle, this.Font, new SolidBrush(this._penColor),0,0);
56         }
57     }
View Code

  这里有两个属性,Angle和PenColor,一个是简单数据类型,只需给他加上DefaultValue特性即可,但要注意默认值的后缀,必须使用类型相关的后缀,否则设置无效。

  一个Color类型的属性,我们无法使用上面的方式,否则编译错误。我们需要给他提供两个函数ResetPenColor和ShouldSerializePenColor,关键在于函数的取名,Reset + 属性名,ShouldSerialize + 属性名。ResetPenColor告诉设计器PenColor的默认值是什么,ShouldSerializePenColor告诉设计器,什么时候将属性进行设计时序列化,这里,我们让其值不为默认值时,进行序列化。

  需要注意的是,设置属性的默认值时,也要设置属性变量的初始值,当然,要与默认值相同。

三.完整代码

  下面是完整的代码:

  
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.ComponentModel;
 6 using System.Drawing;
 7 
 8 namespace TestDefaultProperty
 9 {
10     //控件
11     [DefaultProperty("Angle")]
12     [DefaultEvent("Test")]
13     public class MyControl : System.Windows.Forms.UserControl
14     {
15         private double _angle = 90D;
16         private Color _penColor = Color.Red;
17 
18         public delegate void TestDefaultEvent();
19 
20         public event TestDefaultEvent Test;
21 
22         [BrowsableAttribute(true)]
23         [DefaultValue(90D)]
24         public double Angle
25         {
26             get
27             { return _angle; }
28             set
29             { _angle = value; }
30         }
31 
32         [Browsable(true)]
33         public Color PenColor
34         {
35             get
36             {
37                 return _penColor;
38             }
39             set
40             {
41                 _penColor = value;
42                 Invalidate();
43             }
44         }
45 
46         public void ResetPenColor()
47         {
48             PenColor = Color.Red;
49         }
50 
51         public bool ShouldSerializePenColor()
52         {
53             return PenColor != Color.Red;
54         }
55 
56 
57         public MyControl()
58         {
59             //this._angle = 90;
60         }
61 
62         protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
63         {
64             e.Graphics.DrawString("The Angle is " + _angle, this.Font, new SolidBrush(this._penColor),0,0);
65         }
66     }
67 }
View Code

  新建Windows工程,添加该代码,将MyControl控件拖入Form中,在属性框中查看其属性。

posted on 2015-01-18 12:12  景林  阅读(1369)  评论(0编辑  收藏  举报