Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自定义控件的自定义属性_C#_Properties_Controls - Fatal编程技术网

C# 自定义控件的自定义属性

C# 自定义控件的自定义属性,c#,properties,controls,C#,Properties,Controls,我正在开发一组自定义控件。我有多个要组合在一起的属性 我知道我可以这样做: [Category("HoverStyle"), Description("Specifies if the label will bold when the mouse is hovering.")] public bool HoverBold { get; set; } [Category("HoverStyle"), Description("Specifies if the label will italici

我正在开发一组自定义控件。我有多个要组合在一起的属性

我知道我可以这样做:

[Category("HoverStyle"), Description("Specifies if the label will bold when the mouse is hovering.")]
public bool HoverBold { get; set; }

[Category("HoverStyle"), Description("Specifies if the label will italicize when the mouse is hovering.")]
public bool HoverItalicize { get; set; }

[Category("HoverStyle"), Description("Specifies if the label will underline when the mouse is hovering.")]
public bool HoverUnderline { get; set; }
但按字母顺序组织特性栅格时,它们不会显示在组中。我希望这些属性中的每一个都显示为结构的属性,就像x坐标和y坐标显示在控件的Location属性下一样

我尝试创建如下结构:

public struct HoverStyle
{
    public bool Bold { get; set; }
    public bool Italicize { get; set; }
    public bool Underline { get; set; }
}

但这不是我想要的。谢谢你的帮助

您需要指定它的行为不符合预期。无论如何,请尝试:

[TypeConverter(typeof(ExpandableObjectConverter))]
public struct HoverStyle
{
    public bool Bold { get; set; }
    public bool Italicize { get; set; }
    public bool Underline { get; set; }
}

虽然这不是这个确切场景的答案,但同样的解决方案也适用。在这个解决方案中,我展示了我是如何完成上述工作的,但是使用了一个为我正在绘制的控件定义了TopLeft、TopRight、BottomLeft和BottomRight半径的结构。我创建了自己的结构和类型转换器

[Serializable]
[TypeConverter(typeof(CornerRadiiConverter))]
public struct CornerRadii
{
    private bool all;
    private int topLeft, topRight, bottomLeft, bottomRight;

    public readonly static CornerRadii Empty;

    [RefreshProperties(RefreshProperties.All)]
    public int All
    {
        get
        {
            if (!this.all)
                return -1;
            return this.topLeft;
        }
        set
        {
            if (!this.all || this.topLeft != value)
            {
                this.all = true;
                this.topLeft = value;
                this.topRight = value;
                this.bottomLeft = value;
                this.bottomRight = value;
            }
        }
    }

    [RefreshProperties(RefreshProperties.All)]
    public int TopLeft
    {
        get
        {
            return this.topLeft;
        }
        set
        {
            if (this.all || this.topLeft != value)
            {
                this.all = false;
                this.topLeft = value;
            }
        }
    }

    [RefreshProperties(RefreshProperties.All)]
    public int TopRight
    {
        get
        {
            if (this.all)
                return this.topLeft;
            return this.topRight;
        }
        set
        {
            if (this.all || this.topRight != value)
            {
                this.all = false;
                this.topRight = value;
            }
        }
    }

    [RefreshProperties(RefreshProperties.All)]
    public int BottomLeft
    {
        get
        {
            if (this.all)
                return this.topLeft;
            return this.bottomLeft;
        }
        set
        {
            if (this.all || this.bottomLeft != value)
            {
                this.all = false;
                this.bottomLeft = value;
            }
        }
    }

    [RefreshProperties(RefreshProperties.All)]
    public int BottomRight
    {
        get
        {
            if (this.all)
                return this.topLeft;
            return this.bottomRight;
        }
        set
        {
            if (this.all || this.bottomRight != value)
            {
                this.all = false;
                this.bottomRight = value;
            }
        }
    }

    static CornerRadii()
    {
        CornerRadii.Empty = new CornerRadii(0);
    }

    public CornerRadii(int all)
    {
        this.all = true;
        this.topLeft = all;
        this.topRight = all;
        this.bottomLeft = all;
        this.bottomRight = all;
    }

    public CornerRadii(int topLeft, int topRight, int bottomLeft, int bottomRight)
    {
        this.topLeft = topLeft;
        this.topRight = topRight;
        this.bottomLeft = bottomLeft;
        this.bottomRight = bottomRight;
        this.all = (this.topLeft != this.topRight || this.topLeft != this.bottomRight ? false : this.topLeft == this.bottomRight);
    }

    internal bool ShouldSerializeAll()
    {
        return this.all;
    }
}

public class CornerRadiiConverter : TypeConverter
{
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public CornerRadiiConverter()
    {
    }

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
        {
            return true;
        }
        return base.CanConvertFrom(context, sourceType);
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(InstanceDescriptor))
        {
            return true;
        }
        return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        string str = value as string;
        if (str == null)
            return base.ConvertFrom(context, culture, value);
        str = str.Trim();
        if (str.Length == 0)
            return null;
        if (culture == null)
            culture = CultureInfo.CurrentCulture;
        char delimiter = culture.TextInfo.ListSeparator[0];
        string[] strArray = str.Split(new char[] { delimiter });
        int[] numArray = new int[strArray.Length];
        TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
        for (int i = 0; i < numArray.Length; i++)
            numArray[i] = (int)converter.ConvertFromString(context, culture, strArray[i]);
        if (numArray.Length != 4)
        {
            object[] objArray = new object[] { "value", str, "topLeft, topRight, bottomLeft, bottomRight" };
            throw new ArgumentException();
        }
        return new CornerRadii(numArray[0], numArray[1], numArray[2], numArray[3]);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == null)
            throw new ArgumentNullException("destinationType");
        if (value is CornerRadii)
            if (destinationType == typeof(string))
            {
                CornerRadii cornerRadii = (CornerRadii)value;
                if (culture == null)
                    culture = CultureInfo.CurrentCulture;
                string str = string.Concat(culture.TextInfo.ListSeparator, " ");
                TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
                string[] strArrays = new string[4];
                strArrays[0] = converter.ConvertToString(context, culture, cornerRadii.TopLeft);
                strArrays[1] = converter.ConvertToString(context, culture, cornerRadii.TopRight);
                strArrays[2] = converter.ConvertToString(context, culture, cornerRadii.BottomLeft);
                strArrays[3] = converter.ConvertToString(context, culture, cornerRadii.BottomRight);
                return string.Join(str, strArrays);
            }
            if (destinationType == typeof(InstanceDescriptor))
            {
                CornerRadii cornerRadii1 = (CornerRadii)value;
                if (cornerRadii1.ShouldSerializeAll())
                {
                    ConstructorInfo constructor = typeof(CornerRadii).GetConstructor(new Type[] { typeof(int) });
                    object[] all = new object[] { cornerRadii1.All };
                    return new InstanceDescriptor(constructor, all);
                }
                Type type = typeof(CornerRadii);
                Type[] typeArray = new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) };
                ConstructorInfo constructorInfo = type.GetConstructor(typeArray);
                object[] left = new object[] { cornerRadii1.TopLeft, cornerRadii1.TopRight, cornerRadii1.BottomLeft, cornerRadii1.BottomRight };
                return new InstanceDescriptor(constructorInfo, left);
            }
        return base.ConvertTo(context, culture, value, destinationType);
    }

    public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
    {
        if (context == null)
            throw new ArgumentNullException("context");
        if (propertyValues == null)
            throw new ArgumentNullException("propertyValues");
        CornerRadii value = (CornerRadii)context.PropertyDescriptor.GetValue(context.Instance);
        int item = (int)propertyValues["All"];
        if (value.All != item)
            return new CornerRadii(item);
        return new CornerRadii((int)propertyValues["TopLeft"], (int)propertyValues["TopRight"], (int)propertyValues["BottomLeft"], (int)propertyValues["BottomRight"]);
    }

    public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(CornerRadii), attributes);
        string[] strArrays = new string[] { "All", "TopLeft", "TopRight", "BottomLeft", "BottomRight" };
        return properties.Sort(strArrays);
    }

    public override bool GetPropertiesSupported(ITypeDescriptorContext context)
    {
        return true;
    }
}
[可序列化]
[类型转换器(类型(角半径转换器))]
公共结构半径
{
私人布尔所有;
private int上左、上右、下左、下右;
公共只读静态角半径为空;
[RefreshProperties(RefreshProperties.All)]
公共INTALL
{
得到
{
如果(!this.all)
返回-1;
返回此文件。左上角;
}
设置
{
如果(!this.all | | this.topLeft!=值)
{
这是真的;
this.topLeft=值;
this.topRight=值;
this.bottomLeft=值;
this.bottomRight=值;
}
}
}
[RefreshProperties(RefreshProperties.All)]
公共int左上角
{
得到
{
返回此文件。左上角;
}
设置
{
if(this.all | | this.topLeft!=值)
{
这一切都是假的;
this.topLeft=值;
}
}
}
[RefreshProperties(RefreshProperties.All)]
公共int右上角
{
得到
{
如果(全部)
返回此文件。左上角;
返回此.topRight;
}
设置
{
if(this.all | | this.topRight!=值)
{
这一切都是假的;
this.topRight=值;
}
}
}
[RefreshProperties(RefreshProperties.All)]
公共int左下角
{
得到
{
如果(全部)
返回此文件。左上角;
返回这个。左下角;
}
设置
{
if(this.all | | this.bottomLeft!=值)
{
这一切都是假的;
this.bottomLeft=值;
}
}
}
[RefreshProperties(RefreshProperties.All)]
公共权利
{
得到
{
如果(全部)
返回此文件。左上角;
返回这个。右下角;
}
设置
{
if(this.all | | this.bottomRight!=值)
{
这一切都是假的;
this.bottomRight=值;
}
}
}
静态转弯半径()
{
角半径。空=新的角半径(0);
}
公共角半径(整数)
{
这是真的;
this.topLeft=all;
this.topRight=all;
this.bottomLeft=all;
this.bottomRight=all;
}
公共角半径(int-toplown、int-toplown、int-bottomLeft、int-bottomRight)
{
this.topLeft=topLeft;
this.topRight=topRight;
this.bottomLeft=bottomLeft;
this.bottomRight=bottomRight;
this.all=(this.toplight!=this.toplight | | this.toplight!=this.bottomRight?false:this.toplight==this.bottomRight);
}
内部布尔值应序列化所有()
{
把这个还给我;
}
}
公共类CornerRadiConverter:TypeConverter
{
[TargetedPatchingOptOut(“性能对于跨NGen映像边界内联这种类型的方法至关重要”)]
公共拐角半径转换器()
{
}
公共覆盖布尔CanConvertFrom(ITypeScriptorContext上下文,类型sourceType)
{
if(sourceType==typeof(string))
{
返回true;
}
返回base.CanConvertFrom(上下文,sourceType);
}
公共覆盖布尔CanConvertTo(ITypeDescriptorContext上下文,类型destinationType)
{
if(destinationType==typeof(InstanceDescriptor))
{
返回true;
}
返回base.CanConvertTo(上下文,destinationType);
}
公共重写对象ConvertFrom(ITypeDescriptorContext上下文、CultureInfo区域性、对象值)
{
string str=作为字符串的值;
如果(str==null)
返回base.ConvertFrom(上下文、区域性、值);
str=str.Trim();
如果(str.Length==0)
返回null;
if(区域性==null)
culture=CultureInfo.CurrentCulture;
字符分隔符=culture.TextInfo.ListSeparator[0];
string[]strArray=str.Split(新字符[]{delimiter});
int[]numArray=新int[strArray.Length];
TypeConverter converter=TypeDescriptor.GetConverter(typeof(int));
for(int i=0;i