Winforms 如何在属性网格中显示下拉控件?

Winforms 如何在属性网格中显示下拉控件?,winforms,propertygrid,Winforms,Propertygrid,我正在项目中添加属性网格控件。 我必须在属性网格的一个字段中显示下拉框。 是否有任何解决方案可以应用此问题。您必须在PropertyGrid中为属性声明一个类型编辑器,然后添加到选项列表中。此示例创建一个,然后覆盖GetStandardValues()方法以向下拉列表提供选项: private String _formatString = null; [Category("Display")] [DisplayName("Format String")] [Description("Format

我正在项目中添加属性网格控件。 我必须在属性网格的一个字段中显示下拉框。
是否有任何解决方案可以应用此问题。

您必须在
PropertyGrid
中为属性声明一个类型编辑器,然后添加到选项列表中。此示例创建一个,然后覆盖
GetStandardValues()
方法以向下拉列表提供选项:

private String _formatString = null;
[Category("Display")]
[DisplayName("Format String")]
[Description("Format string governing display of data values.")]
[DefaultValue("")]
[TypeConverter(typeof(FormatStringConverter))]
public String FormatString { get { return _formatString; } set { _formatString = value; } }

public class FormatStringConverter : StringConverter
{
    public override Boolean GetStandardValuesSupported(ITypeDescriptorContext context) { return true; }
    public override Boolean GetStandardValuesExclusive(ITypeDescriptorContext context) { return true; }
    public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        List<String> list = new List<String>();
        list.Add("");                      
        list.Add("Currency");              
        list.Add("Scientific Notation");   
        list.Add("General Number");        
        list.Add("Number");                
        list.Add("Percent");               
        list.Add("Time");
        list.Add("Date");
        return new StandardValuesCollection(list);
    }
}
public enum SummaryOptions
{
    Sum = 1,
    Avg,
    Max,
    Min,
    Count,
    Formula,
    GMean,
    StdDev
}

private SummaryOptions _sumType = SummaryOptions.Sum;
[Category("Summary Values Type")]
[DisplayName("Summary Type")]
[Description("The summary option to be used in calculating each value.")]
[DefaultValue(SummaryOptions.Sum)]
public SummaryOptions SumType { get { return _sumType; } set { _sumType = value; } }
这为您提供了通过覆盖引入自己行为的机会

下面是一个简单的示例,它允许属性的枚举类型自动向
PropertyGrid
下拉列表提供其值:

private String _formatString = null;
[Category("Display")]
[DisplayName("Format String")]
[Description("Format string governing display of data values.")]
[DefaultValue("")]
[TypeConverter(typeof(FormatStringConverter))]
public String FormatString { get { return _formatString; } set { _formatString = value; } }

public class FormatStringConverter : StringConverter
{
    public override Boolean GetStandardValuesSupported(ITypeDescriptorContext context) { return true; }
    public override Boolean GetStandardValuesExclusive(ITypeDescriptorContext context) { return true; }
    public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        List<String> list = new List<String>();
        list.Add("");                      
        list.Add("Currency");              
        list.Add("Scientific Notation");   
        list.Add("General Number");        
        list.Add("Number");                
        list.Add("Percent");               
        list.Add("Time");
        list.Add("Date");
        return new StandardValuesCollection(list);
    }
}
public enum SummaryOptions
{
    Sum = 1,
    Avg,
    Max,
    Min,
    Count,
    Formula,
    GMean,
    StdDev
}

private SummaryOptions _sumType = SummaryOptions.Sum;
[Category("Summary Values Type")]
[DisplayName("Summary Type")]
[Description("The summary option to be used in calculating each value.")]
[DefaultValue(SummaryOptions.Sum)]
public SummaryOptions SumType { get { return _sumType; } set { _sumType = value; } }

由于属性是枚举类型,这些枚举值将自动传递为下拉选项。

GridView
中使用
Template字段
ItemTemplate
。不,这不是grid。这是windows窗体中的PropertyGrid控件。此链接可能比公认的答案更能帮助您->虽然我认为使用列表框可能有点过分,但我认为仅使用内存中的一个好列表就可以完成此任务。使用此链接,如何在下拉列表中显示动态元素?如何将不同属性的自定义列表传递给
FormatStringConverter
?您可能希望将其作为新问题发布。这可能比你意识到的更复杂。如果您真正的意思是“动态”,那么最大的问题可能是在需要更改时刷新它。这更像是一个“抓住它一次”的列表,而不是一个应该不断变化的列表。