我正在使用Dev Express PropertyGrid控件来显示复杂对象。一切看起来都不错,但我希望能够自定义某些方面,所以这里是:
1)当显示项目列表时,对于每一行而不是[0],[1],[2] ...索引,我希望能够显示友好名称。
我为我的Collection实现了ICustomTypeDescriptor接口:
public class CustomObservableCollection : ObservableCollection<PropertyGridWorkSheetViewModel>, ICustomTypeDescriptor
{
public CustomObservableCollection(IEnumerable<PropertyGridWorkSheetViewModel> items) :
base(items)
{ }
public override string ToString() => $"{this.Items.Count()} Worksheets Available";
#region ICustomTypeDescriptor impl
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);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
/// <summary>
/// Called to get the properties of this type. Returns properties with certain
/// attributes. this restriction is not implemented here.
/// </summary>
/// <param name="attributes"></param>
/// <returns></returns>
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return GetProperties();
}
/// <summary>
/// Called to get the properties of this type.
/// </summary>
/// <returns></returns>
public PropertyDescriptorCollection GetProperties()
{
// Create a collection object to hold property descriptors
var pds = new PropertyDescriptorCollection(null);
// Iterate the list of employees
for (int i = 0; i < this.Items.Count; i++)
{
// Create a property descriptor for the employee item and add to the property descriptor collection
var pd = new CustomCollectionPropertyDescriptor(this, i);
pds.Add(pd);
}
// return the property descriptor collection
return pds;
}
#endregion
}
还为我的自定义集合扩展了PropertyDescriptor类,如下所示:
public class CustomCollectionPropertyDescriptor : PropertyDescriptor
{
private CustomObservableCollection collection = null;
private int index = -1;
public CustomCollectionPropertyDescriptor(CustomObservableCollection coll, int idx) :
base("#" + idx.ToString(), null)
{
this.collection = coll;
this.index = idx;
}
public override AttributeCollection Attributes
{
get
{
return new AttributeCollection(null);
}
}
public override bool CanResetValue(object component)
{
return true;
}
public override Type ComponentType
{
get
{
return this.collection.GetType();
}
}
public override string DisplayName => "DisplayName";
public override string Description => "Description";
public override object GetValue(object component)
{
return this.collection[index];
}
public override bool IsReadOnly
{
get { return false; }
}
public override string Name
{
get { return "#" + index.ToString(); }
}
public override Type PropertyType
{
get { return this.collection[index].GetType(); }
}
public override void ResetValue(object component)
{
}
public override bool ShouldSerializeValue(object component)
{
return true;
}
public override void SetValue(object component, object value)
{
// this.collection[index] = value;
}
}
但没有任何成功,Property Grid正在为集合的每一行显示[0],[1] ...索引
第二方面:
我希望能够将我的行数据绑定到我的Collection的每一行上的复选框,然后编写我自己的逻辑,只需使用该复选框。
我附上了一张图片来描述我的两个请求: https://ibb.co/dizwqS