.NET中封装控件时添加属性组,类似于Size(Width,Height)
2010-06-11 09:32
429 查看
本人从来没封装过控件,今天因项目需要,要给控件写一个属性组,就调查研究了一下,调查结果与大家分享,希望对大家有所帮助。
一,先写一个属性组的结构,有了结构就好办事了:)
/// <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; }
}
一,先写一个属性组的结构,有了结构就好办事了:)
/// <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; }
}
相关文章推荐
- xml中搭建布局,当添加控件的时候自动生成的<Button android:layout_width="" android:layout_height="" />变成一行了
- DataGridViewRowHeadersWidthSizeMode属性和ColumnHeadersHeightSizeMode属性
- [扩展]为UIView扩展x,y,width,height,centerX,centerY,Size,Origin等属性
- 使用@property 添加width 和 height 属性
- 关于控件的Left 、Top、Width和Height属性
- ASP.NET控件开发学习笔记--第4回 为控件添加属性
- ASP.NET开发:在用户控件中添加属性
- .NET----给用户控件添加事件
- (javascript)动态添加的控件如何设置其属性
- 在C#中使用属性控件添加属性窗口
- ListView item布局layout_width和layout_height属性
- 动态添加控件导致weight和height失效的解决方法
- EDIT控件如何添加WS_BORDER 属性?
- 在C#中使用属性控件添加属性窗口
- 为控件后台添加属性
- 类似于新浪博客;添加关注的浮动框:使用微软的MopUp控件仿作的
- 常用的.net控件设置时的一些属性
- IE6不支持CSS中的min-width/height属性问题的解决方法
- 【读书笔记】关于onMeasur(int widthMeasureSpec,int heightMeasureSpec)中的mode和size
- 图片上传验证(height,width,size)