如何在属性网格中显示下拉控件? [英] How to show Drop down control in Property Grid?
问题描述
我正在我的项目中添加属性网格控件.我必须在属性网格的一个字段中显示下拉框.有什么解决方案可以应用这个.
I am adding the Property grid control in my project. I have to show the Drop down box in one field of Property Grid. Is there any solution to apply this.
推荐答案
您必须为 PropertyGrid
中的属性声明一个类型编辑器,然后添加到选择列表中.此示例创建一个 类型转换器,然后覆盖 GetStandardValues()
方法为下拉菜单提供选择:
You have to declare a type editor for the property in your PropertyGrid
and then add to the list of choices. This example creates a Type Converter and then overrides the GetStandardValues()
method to provide choices to the drop-down:
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);
}
}
关键是在行中分配了类型转换器的属性:
The key is the property being assigned a Type Converter in the line:
[TypeConverter(typeof(FormatStringConverter))]
这让您有机会通过覆盖来介绍自己的行为.
That provides you with the opportunity to introduce your own behavior via overrides.
这是一个更简单的示例,它允许属性的 Enum 类型自动将其值提供给 PropertyGrid
下拉列表:
Here's a simpler example, which allows the Enum type of a Property to automatically provide its values to the PropertyGrid
drop-down:
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; } }
由于该属性是 Enum 类型,因此这些 enum 值会自动传递成为下拉选项.
By virtue of the fact that the property is an Enum type, those enum values pass through to become the drop-down options automatically.
这篇关于如何在属性网格中显示下拉控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!