是什么在属性网格编辑集合的正确方法 [英] What's the correct way to edit a collection in a property grid
问题描述
我有这显示在属性网格的类。其中一个属性是列表< SOMETYPE方式>
什么是设置代码最简单的/正确的方法这样我就可以添加和通过属性网格中删除从这个集合的项目,最好使用标准的 CollectionEditor
。
其中一个错误的方法是这样的:
用户annakata认为我暴露一个的IEnumerable
接口而不是一个集合。可有人请我提供更多的细节?
我有额外的复杂性,该集合由 GET
返回没有按' ŧ实际指向我班的一员,而是建立在飞从其他成员,是这样的:
公开清单<&SOMETYPE GT;东西
{
得到
{
名单,LT; SOMETYPE>东西=新的List<&SOMETYPE GT;();
//...populate的东西从内部XML树
返回的东西的数据;
}
组
{
//...update利用价值
}
}
的一些数据在内部XML树
这是一个稍微棘手的一个;该解决方案涉及与完整的.NET框架建设(因为仅客户端框架不包括 System.Design
)。您需要创建自己的 CollectionEditor
的子类,告诉它做什么用的临时收集的用户界面与它完成之后:
公共类SomeTypeEditor:CollectionEditor {
公共SomeTypeEditor(类型类型):基地(类型){}
公覆盖对象的EditValue(ITypeDescriptorContext背景下,IServiceProvider的供应商,对象的值){
对象result = base.EditValue(背景下,供应商,价值);
//分配从UI到属性
((ClassContainingStuffProperty)context.Instance).Stuff =临时的集合(list<&SOMETYPE GT;)的结果;
返回结果;
}
}
然后,你必须来装饰与你的财产 EditorAttribute
:
公开名单<&SOMETYPE GT;东西{
// ...
}
长而曲折的,是,但它的作品。你后点击集合编辑器弹出的确定,就可以再次打开它和值将保持
请注意:您需要导入的命名空间 System.ComponentModel
, System.ComponentModel.Design
和 System.Drawing.Design
I have a class which is displayed in a property grid. One of the properties is a List<SomeType>
.
What's the easiest/correct way to set up the code so that I can add and remove items from this collection through the property grid, preferably using the standard CollectionEditor
.
One of the wrong ways is like this:
set not being called when editing a collection
User annakata suggest that I expose an IEnumerable
interface instead of a collection. Can someone please provide me with more details?
I have the extra complication that the collection returned by get
doesn't actually point to a member in my class, but is build on the fly from an other member, something like this:
public List<SomeType> Stuff
{
get
{
List<SomeType> stuff = new List<SomeType>();
//...populate stuff with data from an internal xml-tree
return stuff;
}
set
{
//...update some data in the internal xml-tree using value
}
}
This is a slightly tricky one; the solution involves building with the full .NET Framework (since the client-only framework doesn't include System.Design
). You need to create your own subclass of CollectionEditor
and tell it what to do with the temporary collection after the UI is finished with it:
public class SomeTypeEditor : CollectionEditor {
public SomeTypeEditor(Type type) : base(type) { }
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
object result = base.EditValue(context, provider, value);
// assign the temporary collection from the UI to the property
((ClassContainingStuffProperty)context.Instance).Stuff = (List<SomeType>)result;
return result;
}
}
Then you have to decorate your property with the EditorAttribute
:
[Editor(typeof(SomeTypeEditor), typeof(UITypeEditor))]
public List<SomeType> Stuff {
// ...
}
Long and convoluted, yes, but it works. After you click "OK" on the collection editor popup, you can open it again and the values will remain.
Note: You need to import the namespaces System.ComponentModel
, System.ComponentModel.Design
and System.Drawing.Design
.
这篇关于是什么在属性网格编辑集合的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!