.NET中封装控件时添加属性组,类似于Size(Width,Height)
来源:互联网 发布:人工智能世界知乎 编辑:程序博客网 时间:2024/06/05 09:10
本人从来没封装过控件,今天因项目需要,要给控件写一个属性组,就调查研究了一下,调查结果与大家分享,希望对大家有所帮助。
一,先写一个属性组的结构,有了结构就好办事了:)
/// <summary>
/// Key Container
/// </summary>
[TypeConverter(typeof(KeyContainerTypeConvert))]
public class KeyContainer : Component
{
/// <summary>
/// Left
/// </summary>
[DefaultValue(true),
NotifyParentProperty(true),
RefreshProperties(RefreshProperties.Repaint)]
public Boolean Left
{
get { return pbLeft; }
set { pbLeft = value; }
}
/// <summary>
/// Right
/// </summary>
[DefaultValue(true),
NotifyParentProperty(true),
RefreshProperties(RefreshProperties.Repaint)]
public Boolean Right
{
get { return pbRight; }
set { pbRight = value; }
}
private Boolean pbLeft = true;
private Boolean pbRight = true;
}
二,现在开始写最关键的类KeyContainerTypeConvert ,他要继承ExpandableObjectConverter。
/// <summary>
/// KeyContainerTypeConvert
/// </summary>
public class KeyContainerTypeConvert : ExpandableObjectConverter
{
/// <summary>
/// ToString
/// </summary>
private string ToString(object value)
{
KeyContainer c = (KeyContainer)value;
BooleanConverter ic = new BooleanConverter();
return string.Format("{0},{1}",
ic.ConvertToString(c.Left),
ic.ConvertToString(c.Right));
}
/// <summary>
/// FromString
/// </summary>
private KeyContainer FromString(object value)
{
string[] values = ((string)value).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
if (values.Length != 2)
throw new ArgumentException("Could not convert the value");
try
{
KeyContainer cts = new KeyContainer();
BooleanConverter ic = new BooleanConverter();
cts.Left = (Boolean)ic.ConvertFromString(values[0]);
cts.Right = (Boolean)ic.ConvertFromString(values[1]);
return cts;
}
catch
{
throw new ArgumentException("Could not convert the value");
}
}
/// <summary>
/// CanConvertFrom
/// </summary>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
else
{
return base.CanConvertFrom(context, sourceType);
}
}
/// <summary>
/// ConvertFrom
/// </summary>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
return FromString(value);
}
else
{
return base.ConvertFrom(context, culture, value);
}
}
/// <summary>
/// CanConvertTo
/// </summary>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
else
{
return base.CanConvertTo(context, destinationType);
}
}
/// <summary>
/// ConvertTo
/// </summary>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
return ToString(value);
}
else
{
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
三,最后就是定义对象和具体试用了。
/// <summary>
/// 公用KeyActions
/// </summary>
private KeyContainer pbUseCommonKeyActions = new KeyContainer();
/// <summary>
/// 公用KeyActions的取得和设定
/// </summary>
[Browsable(true), Category("自定义"),
Description("公用KeyActions的取得和设定"),
DefaultValue(typeof(KeyContainer), ""),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[TypeConverter(typeof(KeyContainer))]
public KeyContainer UseCommonKeyActions
{
get { return pbUseCommonKeyActions; }
set { pbUseCommonKeyActions = value; }
}
- .NET中封装控件时添加属性组,类似于Size(Width,Height)
- 使用@property 添加width 和 height 属性
- 关于控件的Left 、Top、Width和Height属性
- HTML中<table>中width和height属性的详解(表格)
- javascript控制css中style.width和style.height属性时单位px的问题
- 如何在自定义控件中获得width和height?
- 取得控件的width,height
- android动态获取TextView的属性(width、height、line)
- Assertion failed (size.width>0 && size.height>0)
- 如何让自定义控件原来的width和height属性可用
- 火狐浏览器中Div为空时height,width不起作用
- Screen width、height、availWidth、availHeight属性
- Screen width、height、availWidth、availHeight属性
- Screen width、height、availWidth、availHeight属性
- img的width和height属性
- div的width和height属性
- Android中shape标签里的size子标签的android:width和android:height的使用
- CSS学习之width属性 height属性和Padding(内边距)
- 6大B2C购物系统比较
- C#语法糖(Csharp Syntactic sugar)大汇总
- 升级听力助手
- 李健 - 似水流年
- 处理rm 空间不释放的问题
- .NET中封装控件时添加属性组,类似于Size(Width,Height)
- ExtJS提交服务器的三种方式
- NAND Flash ECC算法长度计算
- 网站备案6小时通过
- 转码用lame,标签用easyTag,mp3轻松搞定
- (2010-06-11)浅谈在Oracle中如何利用REDO实现故障恢复
- W3C 验证的是是非非
- 文件加密解密演示
- 关于链式队列是否需要头结点