开发者

User-defined Controls and Their Properties

开发者 https://www.devze.com 2023-04-09 16:22 出处:网络
I am working on a user-defined button, this has two custom properties like this: public enum FirstType

I am working on a user-defined button, this has two custom properties like this:

public enum FirstType
{
    egg,
    leg
}
// first property:
public FirstType FirstProperty { get; set; }

and I have a base class and 5 derived classes of this base class, and the second property will refer to one of the开发者_Go百科se 5,

//second property
public BaseClass SecondProperty { get; set; }

Now my question is: how can I have a drop down list of these 5 classes for the second property in properties window like the first one? Is it possible?


For that property you need to create your own custom TypeConverter and override GetStandardValues

This is your property:

[TypeConverter(typeof(MyTypeConverter)]
public BaseClass SecondProperty { get; set; }

This is your type converter:

public class MyTypeConverter : TypeConverter
{
    ...

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

    /// <summary>
    /// select only from list
    /// </summary>
    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
        return true;
    }

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        return new StandardValuesCollection(/* list of derived classes */);
    }
}


OK,I used enum to solve my problem, first a property of this enum type, then called those classes in set statement of the property. Thanks to all.

0

精彩评论

暂无评论...
验证码 换一张
取 消