Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将BindingList绑定到集合_C#_.net_Bindinglist - Fatal编程技术网

C# 将BindingList绑定到集合

C# 将BindingList绑定到集合,c#,.net,bindinglist,C#,.net,Bindinglist,我有一个库,它返回如下集合: 公共IEnumerable警报{..} 我想把这个集合转换成一个BindingList,以便在GUI中使用。保持BindingList与IEnumerable集合同步的最佳方法是什么 编辑:对于这个问题,假设我对库没有控制权,实际实现使用一个列表。 但我不想触碰这个密码 这个库还有一个很好的界面,包含AddAlert、RemoveAlert等。让GUI与所有这些更改保持同步的最佳方法是什么?假设您正在包装的类公开了Insert之类的内容,您应该能够从BindingL

我有一个库,它返回如下集合:

公共IEnumerable警报{..}

我想把这个集合转换成一个BindingList,以便在GUI中使用。保持BindingList与IEnumerable集合同步的最佳方法是什么

编辑:对于这个问题,假设我对库没有控制权,实际实现使用一个列表。
但我不想触碰这个密码


这个库还有一个很好的界面,包含AddAlert、RemoveAlert等。让GUI与所有这些更改保持同步的最佳方法是什么?

假设您正在包装的类公开了
Insert
之类的内容,您应该能够从
BindingList
派生,覆盖一些关键方法,例如:

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
}
class MyList:BindingList
{
私有只读Foo包装;
公共MyList(Foo-wrapped)
:基本(新列表(包装的项目))
{
this.wrapped=wrapped;
}
受保护的覆盖无效插入项(int索引,T项)
{
包装。插入(索引,项目);
基本插入项(索引,项目);
}
受保护的覆盖void removietem(int索引)
{
包装。移除(此[索引]);
基本删除项(索引);
}
受保护的覆盖无效ClearItems()
{
包装好的;
base.ClearItems();
}
//也可能是设置项
}

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

谢谢,但这不太管用。如果库中的集合以其他方式(不是通过用户通过GUI)更改,列表框将不会与新集合同步。