我正在使用 C# 中的 Compact Framework 3.5 为 Windows Mobile 6 设备编写自定义控件。我正在编写的控件代表一个按钮列表,我们称之为ButtonList和按钮ButtonListItem。
ButtonListItem 的列表可以在设计器中通过集合编辑器进行编辑:
我对其进行了设置,以便在集合编辑器中添加或删除 ButtonListItem 时更新和重新绘制 ButtonList。
这是问题所在:
当我通过用鼠标选择它并在设计器中按下删除按钮手动删除 ButtonListItem 时,ButtonList 的内部状态不会更新。
我的目标是使设计器中的手动删除行为类似于集合编辑器中的删除。
一个例子是 TabControl 类,其中在集合编辑器中删除标签页和手动删除的行为完全相同。
您将如何实现这一目标?
编辑 1:用于向 ButtonList 添加和删除 ButtonListItem 的简化代码
class ButtonList : ContainerControl
{
public ButtonListItemCollection Items { get; private set; } // Implements IList and has events for adding and removing items
public ButtonList()
{
this.Items.ItemAdded += new EventHandler<ButtonListItemEventArgs>(
delegate(object sender, ButtonListItemEventArgs e)
{
// Set position of e.Item, do more stuff with e.Item ...
this.Controls.Add(e.Item);
});
this.Items.ItemRemoved += new EventHandler<ButtonListItemEventArgs>(
delegate(object sender, ButtonListItemEventArgs e)
{
this.Controls.Remove(e.Item);
});
}
}