我有一个自定义控件,有一个 Items 财产。我已经应用了 EditorAttributeUITypeEditor 类型 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 按钮 CollectionEditor 六个 Items 属性没有被调用。

当从 EditValue 方法 UITypeEditor 属性应该调用属性的设置器块。

这使我发疯。我什至尝试添加 EventListItemsCollection, ,因此,当添加项目时,我可以使用控件的UI进行任何想法。

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

有帮助吗?

解决方案

我尝试重述您的情况:使用以下代码,每当我从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; }
    }

其他提示

收集属性应仅阅读。这是通过Getter检索并进行调整的集合。 Setter永远不会进入它,因为这意味着要设置一个新集合。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top