简体   繁体   中英

How to create Public String property with drop-down list of options?

Is it possible to attach a List of strings to a String property so that the user can select one of the strings from the Properties window? Should I implement ICollection or something of that sort?

If you are trying to restrict a property to one of a few specific options, you should use an Enum instead of a String for the property.

If you want to provide defaults, but let them type any string in and ignore the defaults, then you can use StringConverter. For details, read Getting the Most Out of the .NET Property Grid control . It covers this exact scenario.

No. You should create an enum type with your string choices, and make the property of that type. Example:

public enum Choices
{
    NiceChoice,
    PoorChoice
}

public class Chooser
{
    public Choices Choice { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM