C# 如何使用自定义属性在propertygrid中添加动态组合框属性?
我必须根据一些动作在propertygrid中创建一些动态属性。我能够按照下面的示例创建动态属性。但是我还需要在propertygrid中添加一个组合框。为此,我创建了一个从stringConverter派生的类。如下图所示:C# 如何使用自定义属性在propertygrid中添加动态组合框属性?,c#,winforms,wpf-controls,propertygrid,C#,Winforms,Wpf Controls,Propertygrid,我必须根据一些动作在propertygrid中创建一些动态属性。我能够按照下面的示例创建动态属性。但是我还需要在propertygrid中添加一个组合框。为此,我创建了一个从stringConverter派生的类。如下图所示: public class FormatStringConverter : StringConverter { public override Boolean GetStandardValuesSupported(ITypeDescriptorContext co
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("DepartmentA");
list.Add("DepartmentB");
list.Add("DepartmentC");
return new StandardValuesCollection(list);
}
}
如果有人用其他类型转换器解决了这个问题,我会告诉你这是否有助于我的事业 这里可以参考Marc Gravell的例子。 我不得不调整其他人的代码以使下拉列表正常工作,如果这不是一个完整的列表,我深表歉意
public Type Type
{
get
{
if (IsDropDownEnabled)
return typeof(DecoratedDropdown);
else
return typeof(this);
}
}
// also necessary to make propertyInfo work
// this allows us to assign lists to propertyGrid dropdowns
[TypeConverter(typeof(PrebuiltListConverter))]
public class DecoratedDropdown { }
public class PrebuiltListConverter : StringConverter
{
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
CvarPropertyDescriptor descriptor = (CvarPropertyDescriptor)context.PropertyDescriptor;
return new StandardValuesCollection(descriptor.Options);
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; }
// credit Zlatko; return false in StringConverter subclass to make editable combobox
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) { return false; }
}
public override Type PropertyType { get { return m_Property.Type; } }
public Type Type
{
get
{
if (IsDropDownEnabled)
return typeof(DecoratedDropdown);
else
return typeof(this);
}
}
public ICollection Options { get { dynamic dObj = m_Property; return dObj.PossibleValues; } }
public String GetClassName() { return TypeDescriptor.GetClassName(this, true); }
public AttributeCollection GetAttributes() { return TypeDescriptor.GetAttributes(this, true); }
public String GetComponentName() { return TypeDescriptor.GetComponentName(this, true); }
public TypeConverter GetConverter() { return TypeDescriptor.GetConverter(this, true); }
public EventDescriptor GetDefaultEvent() { return TypeDescriptor.GetDefaultEvent(this, true); }
public PropertyDescriptor GetDefaultProperty() { return TypeDescriptor.GetDefaultProperty(this, true); }
public object GetEditor(Type editorBaseType) { return TypeDescriptor.GetEditor(this, editorBaseType, true); }
public EventDescriptorCollection GetEvents(Attribute[] attributes) { return TypeDescriptor.GetEvents(this, attributes, true); }
public EventDescriptorCollection GetEvents() { return TypeDescriptor.GetEvents(this, true); }
// GetProperties(Attribute[] attributes) etc.
public PropertyDescriptorCollection GetProperties() { return TypeDescriptor.GetProperties(this, true); }
public object GetPropertyOwner(PropertyDescriptor pd) { return this; }