1

Items我有一个具有属性的自定义控件。我已经应用了EditorAttribute一个UITypeEditor类型的CollectionEditor

收藏类型:

[Serializable]
[Editor(typeof(CollectionEditor), typeof(UITypeEditor))]
public class ListItemsCollection : CollectionBase
{
    // methods
}

控件中的属性声明:

private new ListItemsCollection _Items;

[Editor(typeof(CollectionEditor), typeof(UITypeEditor))]
public new ListItemsCollection Items
{
    get
    {
        return _Items;
    }
    set
    {
        _Items = value;

        // do other UI changes
    }
}

问题:
当我将此控件放到设计器表面时,我可以使用PropertyGrid. 但是,当我单击属性设置器的Ok按钮时,不会被调用。CollectionEditorItems

AFAIK 当从类的EditValue方法返回值时UITypeEditor,应该调用属性的 setter 块。

这让我发疯。我什至尝试在 中添加Event's ListItemsCollection,以便在添加项目时,我可以使用控件的 ui 来做任何我想要的事情。

这应该不难。我究竟做错了什么?

4

2 回答 2

1

Collection properties should be read-only. It's the collection that is retrieved through the getter, and adjusted. The setter never enters into it, because that would mean setting a new collection.

于 2010-09-20T12:20:24.953 回答
1

我尝试重现您的情况:使用以下代码,每当我从 VS 属性窗口编辑列表时,都会显示一个消息框。请注意,您必须自己创建列表。如果你不创建它,VS会创建一个临时列表,你可以从属性窗口编辑它,但不会将你的属性设置到这个列表中(所以你的设置器永远不会被调用)

    public UserControl1()
    {
        InitializeComponent();
        list = new BindingList<ListViewItem>();
        list.ListChanged += new ListChangedEventHandler(list_ListChanged);
    }

    void list_ListChanged(object sender, ListChangedEventArgs e)
    {
        MessageBox.Show(e.ListChangedType.ToString());
    }

    private BindingList<ListViewItem> list;

    public BindingList<ListViewItem> List1
    {
        get { return list; }
    }
于 2010-09-20T12:36:14.747 回答