.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; }
}

原创粉丝点击