繁体   English   中英

将BindingList绑定到集合

[英]Binding a BindingList to a collection

我有一个图书馆,它返回这样的集合:

IEnumerable公共警报{..}

我想将此集合转换为BindingList以在GUI中使用。 使BindingList与IEnumerable集合保持同步的最佳方法是什么?

编辑:对于此问题,假定我无法控制该库,并且实际实现使用List。
但是我不想碰这段代码。

该库还具有与AddAlert,RemoveAlert等的良好接口。使GUI与所有这些更改保持同步的最佳方法是什么?

假设您要包装的类公开了Insert ,那么您应该能够从BindingList<T>派生出来,并覆盖一些关键方法,例如:

class MyList<T> : BindingList<T>
{
    private readonly Foo<T> wrapped;
    public MyList(Foo<T> wrapped)
        : base(new List<T>(wrapped.Items))
    {
        this.wrapped = wrapped;
    }
    protected override void InsertItem(int index, T item)
    {
        wrapped.Insert(index, item);
        base.InsertItem(index, item);            
    }
    protected override void RemoveItem(int index)
    {
        wrapped.Remove(this[index]);
        base.RemoveItem(index);
    }
    protected override void ClearItems()
    {
        wrapped.Clear();
        base.ClearItems();
    }
    // possibly also SetItem
}

这将导致列表在您操作它们时保持同步。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM