有没有办法使子列表与主列表的内容保持同步?

发布于 2024-12-23 05:57:54 字数 105 浏览 2 评论 0原文

我将有一个“主列表”(最好是 BindingList 类型)。在另一个课程中,我有一个子列表,它由“主列表”的某些元素组成。类的每个实例都有不同的元素。有没有办法使每个子列表与“主列表”保持同步?

I will have one 'master list' (preferably of type BindingList). In another class I have a sublist, which is composed of certain elements of the 'master list'. Each instance of the class has different elements. Is there a way to keep each of the sublists synchronized with the 'master list'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

几度春秋 2024-12-30 05:57:54

您可以通过扩展列表并添加复合对象来创建所需的列表类型,并在更新“主”列表时同步这些对象。

这是我在 java 中的做法,你可以在 c# 中做同样的事情,但我不记得确切的语法。您当然可以扩展您喜欢的任何列表类型。

public class MyList extends ArrayList {
    private List<Object> someOtherList;
    public MyList(){
        super();
    }

    public void setSyncList(List<Object> list){
        someOtherList = list;
    }

    @Override
    public boolean add(Object arg0) {
        boolean b = super.add(arg0);
        someOtherList.add(arg0); // here you can decide what action should be done to the syncList, it might not want to add all elements, same goes for the remove below
        return b;
    }

    @Override
    public boolean remove(Object arg0) {
        boolean b = super.remove(arg0);
        someOtherList.remove(arg0);
        return b;
    }

}

You could create that type of list you need by extending your list and add composite objects that you also synchronize whenever you update your "main" list.

Here is how I would do it in java, you can do the same in c# but right know i dont remember exact syntax. You could ofcourse extend what ever list type you like.

public class MyList extends ArrayList {
    private List<Object> someOtherList;
    public MyList(){
        super();
    }

    public void setSyncList(List<Object> list){
        someOtherList = list;
    }

    @Override
    public boolean add(Object arg0) {
        boolean b = super.add(arg0);
        someOtherList.add(arg0); // here you can decide what action should be done to the syncList, it might not want to add all elements, same goes for the remove below
        return b;
    }

    @Override
    public boolean remove(Object arg0) {
        boolean b = super.remove(arg0);
        someOtherList.remove(arg0);
        return b;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文