自定义列表类型的集合编辑器 [英] Collection Editor for Custom List type

查看:95
本文介绍了自定义列表类型的集合编辑器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用VS社区2015.



我创建了一个线程安全列表(TList< T>),它实现了线程中的所有标准列表功能 - 安全的方式。我现在有一个具有TList< ImageEntry>的用户控件。作为财产。 ImageEntry是从Component派生的对象,具有3个属性。但是,CollectionEditor不允许在设计器中维护此列表。



TList:

Using VS Community 2015.

I have create a thread-safe list (TList<T>) that implements all of the standard list functionality in a thread-safe manner. I now have a user control that has a TList<ImageEntry> as a property. ImageEntry is an object derived from Component, with 3 properties. However, the CollectionEditor does not allow maintenance of this list in the designer.

The TList:

public class TList<T> : IList<T>, ICollection<T>, IEnumerable<T>, IDisposable {

private ReaderWriterLockSlim _lck = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
private List<T> _list = new List<T>();
private bool _lockableEnumerator = false;

public TList() { }
public TList(bool lockableEnumerator) : base() {
	_lockableEnumerator = lockableEnumerator;
}
public TList(IEnumerable<T> elements, bool lockableEnumerator = false) {
	_list.AddRange(elements);
	_lockableEnumerator = lockableEnumerator;
}
.... // All the list functionality here
}



内容:


The contents:

[DesignTimeVisible(false)]
public class ImageEntry : Component { 
	protected Bitmap _image = null;
	protected string _path = null;
	ImageDrawMode _sizemode = ImageDrawMode.Zoom;

	public ImageEntry() { }

	[DefaultValue(typeof(Bitmap), null)]
	public virtual Bitmap Image {
		get {
			if (_image == null) {
				if (!string.IsNullOrEmpty(_path))
					_image = new Bitmap(_path);
			}
			return _image;
		}
		set {
			_image = value;
			if (value != null)
				_path = null;
		}
	}
	[DefaultValue(typeof(string), "")]
	public virtual string Path {
		get { return _path ?? string.Empty; }
		set {
			if (string.IsNullOrEmpty(value))
				_path = null;
			else {
				_image = null;
				_path = value;
			}
		}
	}
	[DefaultValue(ImageDrawMode.Zoom)]
	public virtual ImageDrawMode SizeMode {
		get {
			return _sizemode;
		}
		set {
			_sizemode = value;
		}
	}
	public override string ToString() {
		return string.IsNullOrEmpty(_path) ? ((_image == null) ? null : _image.ToString()) : _path;
	}
}



财产:


The property:

TList<ImageEntry> _images = new TList<ImageEntry>();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Description("The collection of images to cycle through")]
[Category("Effects")]
[Editor(typeof(CollectionEditor), typeof(UITypeEditor))]
public TList<ImageEntry> Images {
	get { return _images; }
}





当我在IDE中调出CollectionEditor时,它不会在右边显示ImageEntry属性,并在左侧禁用添加和删除按钮。但是,如果我修改Images属性使其返回内部List< ImageEntry>,它一切正常(但当然,我的Images属性不是线程安全的)。



我尝试过组合一个自定义的Collection编辑器,但这也不起作用。



When I bring up the CollectionEditor in the IDE, it does not show the ImageEntry properties on the right, and the Add and Remove buttons are disabled on the left. However, if I modify the Images property such that it returns the internal List<ImageEntry>, it all works fine (but then, of course, my Images property is not thread-safe).

I have tried putting together a custom Collection editor, but that didn't work either.

public class ImageEntryCollectionEditor : CollectionEditor {
	public ImageEntryCollectionEditor(Type type) : base(type) { }
	protected override Type[] CreateNewItemTypes() {
		return new Type[] { typeof(ImageEntry) };
	}
	protected override Type CreateCollectionItemType() {
		return typeof(ImageEntry);
	}
}





我在这里缺少什么?



What am I missing here?

推荐答案

让UITypeEditor与自定义集合配合使用可能很棘手。

我认为这篇CP文章可以帮到你。

如何编辑和保留集合with CollectionEditor [ ^ ]



这是我自己用过的另一篇CP文章在PropertyGrid中自定义显示集合数据 [ ^ ]



如果你还没有看过,这篇MSDN文章也会有所帮助。 演练:实现UI类型编辑器 [ ^ ]
It can be tricky to get the UITypeEditor to work well with custom collections.
I think this CP article can help you.
How to Edit and Persist Collections with CollectionEditor[^]

This is another CP article I have used myself Customized display of collection data in a PropertyGrid[^]

This MSDN article can also be helpful, if you haven't seen it already. Walkthrough: Implementing a UI Type Editor[^]


我知道了 - 我需要还要在集合上实现非通用IList接口。一旦我这样做,一切正常。
I've got it - I needed to also implement the non-generic IList interface on the collection. As soon as I did that, everything just worked.


这篇关于自定义列表类型的集合编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆