![](/img/trans.png)
[英]How to modify PropertyGrid at runtime (add/remove property and dynamic types/enums)
[英]How to add dynamic combobox properties in propertygrid using custom property?
我必須根據某些操作在 propertygrid 中創建一些動態屬性。 我能夠按照https://www.codeproject.com/Articles/9280/Add-Remove-Items-to-from-PropertyGrid-at-Runtime示例創建動態屬性。 但我還需要在 propertygrid 中添加一個組合框。 為此,我創建了一個從 stringConverter 派生的類。 像下面這樣:
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);
}
}
但是在示例的 customClass 中,我應該返回什么來將 stringConverters 值顯示為 propertygrid 中的組合框?
TypeConverter GetConverter()
{
if(prop.Name == "department")
//what to return here?
else
return TypeDescriptor.GetConverter(this, true);
}
如果有人使用其他 TypeConverters 解決了它,我會感謝它是否有助於我的事業。
您可以在此處參考 Marc Gravell 的示例。 數據綁定動態數據我不得不調整其他人的代碼才能使下拉列表工作,如果這不是一個完整的列表,我深表歉意。
您的自定義類的 GetConverter() 方法應該只返回 TypeDescriptor.GetConverter(this, true)。
如果您不知道,您的自定義類的動態屬性將無法與 DataGridView 一起使用,因為 DGV 讀取它們的方式。 所以我在這里的實現僅限於 PropertyGrid。 嘗試將 DataGridView 與 ICustomTypeDescriptor 一起使用
決定 PropertyGrid 組合框是否應該是可編輯的。 如何將可編輯的組合框添加到 System.Windows.Forms.PropertyGrid?
// 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; } }
我在自定義屬性類上使用 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; }
如果我錯過了什么,請告訴我。 我最近做了這個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.