我正在尝试制作一个WinForms用户控件,其中包含一个
用户控件:
我已经尝试过以下方法:
1. 将Step类作为组件,如此处所述:http://www.codeproject.com/Articles/5372/How-to-Edit-and-Persist-Collections-with-Collectio 2. 将
3. 将DesignerSerializationVisibility设置为Content,如此处所述:Collection Editor within a User Control at Design Time。当它设置为Visible时,设计师将null分配给我的属性;当它设置为Content时,设计师不会分配任何东西。
4. 我还发现了这个:How to make a UserControl with a Collection that can be edited at design time?,但是CollectionBase类已经为我完成了这项工作。
5. 进行了大量调试,但由于没有异常,我真的不知道出了什么问题。当我向collectionForm的关闭事件添加事件侦听器时,即使我在集合编辑器中添加了一些步骤,我也可以看到collectionForm的EditValue属性仍然为null。但我也不知道这是为什么...
在完成这篇文章时,我刚发现了这个主题:Simplest way to edit a collection in DesignMode?。这正是我遇到的相同问题,但是我不能使用所提出的答案,因为我没有使用标准集合。
Collection<T>
属性(其中T代表一些自定义类)。我已经阅读了很多关于这个主题的内容,但是我无法在设计时正常工作(运行时一切正常)。更精确地说:当我点击属性窗口中的“…”按钮时,集合编辑器显示得很好,我可以添加和删除项目。但是当我单击“确定”按钮时,什么也不会发生,并且当我重新打开集合编辑器时,所有项目都丢失了。当我查看设计器文件时,我发现我的属性被分配为null,而不是组合的集合。下面是最重要的代码:用户控件:
[Browsable(true),
Description("The different steps displayed in the control."),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Editor(typeof(CustomCollectionEditor), typeof(UITypeEditor))]
public StepCollection Steps
{
get
{
return wizardSteps;
}
set
{
wizardSteps = value;
UpdateView(true);
}
}
StepCollection class:
public class StepCollection : System.Collections.CollectionBase
{
public StepCollection() : base() { }
public void Add(Step item) { List.Add(item); }
public void Remove(int index) { List.RemoveAt(index); }
public Step this[int index]
{
get { return (Step)List[index]; }
}
}
Step class:
[ToolboxItem(false),
DesignTimeVisible(false),
Serializable()]
public class Step : Component
{
public Step(string name) : this(name, null, StepLayout.DEFAULT_LAYOUT){ }
public Step(string name, Collection<Step> subSteps) : this(name, subSteps, StepLayout.DEFAULT_LAYOUT){ }
public Step(string name, Collection<Step> subSteps, StepLayout stepLayout)
{
this.Name = name;
this.SubSteps = subSteps;
this.Layout = stepLayout;
}
// In order to provide design-time support, a default constructor without parameters is required:
public static int NEW_ITEM_ID = 1;
public Step()
: this("Step" + NEW_ITEM_ID, null, StepLayout.DEFAULT_LAYOUT)
{
NEW_ITEM_ID++;
}
// Some more properties
}
CustomCollectionEditor:
class CustomCollectionEditor : CollectionEditor
{
private ITypeDescriptorContext mContext;
public CustomCollectionEditor(Type type) : base(type) { }
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
mContext = context;
return base.EditValue(context, provider, value);
}
protected override object CreateInstance(Type itemType)
{
if (itemType == typeof(Step))
{
Step s = (Step)base.CreateInstance(itemType);
s.parentContext = mContext; // Each step needs a reference to its parentContext at design time
return s;
}
return base.CreateInstance(itemType);
}
}
我已经尝试过以下方法:
1. 将Step类作为组件,如此处所述:http://www.codeproject.com/Articles/5372/How-to-Edit-and-Persist-Collections-with-Collectio 2. 将
Collection<Step>
更改为自定义集合类StepCollection
,继承自System.Collections.CollectionBase(也在前面的代码项目文章中描述)。3. 将DesignerSerializationVisibility设置为Content,如此处所述:Collection Editor within a User Control at Design Time。当它设置为Visible时,设计师将null分配给我的属性;当它设置为Content时,设计师不会分配任何东西。
4. 我还发现了这个:How to make a UserControl with a Collection that can be edited at design time?,但是CollectionBase类已经为我完成了这项工作。
5. 进行了大量调试,但由于没有异常,我真的不知道出了什么问题。当我向collectionForm的关闭事件添加事件侦听器时,即使我在集合编辑器中添加了一些步骤,我也可以看到collectionForm的EditValue属性仍然为null。但我也不知道这是为什么...
在完成这篇文章时,我刚发现了这个主题:Simplest way to edit a collection in DesignMode?。这正是我遇到的相同问题,但是我不能使用所提出的答案,因为我没有使用标准集合。