组合多个列表

发布于 2024-12-16 20:29:20 字数 2509 浏览 0 评论 0原文

我有 5 组列表,我将必须运行 5 个不同的线程来处理这 5 个列表。我还想在数据网格中显示它们的进展,这将需要一个“组合”列表进行绑定。

不知道从哪里开始。如果我使用 concat 那么如果我删除子列表上的项目,父列表将不会更新。

public partial class MainWindow : Window
{


    ObservableCollection<TestObject> _combinedList = new ObservableCollection<TestObject>();
    public ObservableCollection<TestObject> CombinedList
    {
        get
        {
            return _combinedList;
        }
        set
        {
            _combinedList = value;
        }
    }


    List<TestObject> _list1 = new List<TestObject>();
    List<TestObject> _list2 = new List<TestObject>();
    List<TestObject> _list3 = new List<TestObject>();
    List<TestObject> _list4 = new List<TestObject>();
    List<TestObject> _list5 = new List<TestObject>();


    public MainWindow()
    {
        InitializeComponent();

        dataGridCombined.ItemsSource = CombinedList;

    }

    private void cmdCreateLists_Click(object sender, RoutedEventArgs e)
    {
        createLists();
    }

    public void createLists()
    {
        for (int i = 1; i < 6; i++)
        {
            _list1.Add(new TestObject(i));
        }

        for (int i = 1; i < 6; i++)
        {
            _list2.Add(new TestObject(i*3));
        }

        for (int i = 1; i < 6; i++)
        {
            _list3.Add(new TestObject(i*5));
        }

        for (int i = 1; i < 6; i++)
        {
            _list4.Add(new TestObject(i*7));
        }

        for (int i = 1; i < 6; i++)
        {
            _list5.Add(new TestObject(i*9));
        }

    }

    private void cmdMergeLists_Click(object sender, RoutedEventArgs e)
    {
        //Doesn't work and if i remove item from sublist, CombinedList doesn't update.
        CombinedList = _list1.Concat<TestObject>(_list2);


    }

}

public class TestObject : BindableObject
{
    private int _number;
    public int Number
    {
        get
        {
            return _number;
        }
        set
        {
            _number = value;
            RaisePropertyChanged("Number");
        }
    }

    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
            RaisePropertyChanged("Name");
        }
    }

    public TestObject(int inNum)
    {
        _number = inNum;
        _name = "John";
    }

}

我必须在这里编写某种自定义集合吗?

问候

I have 5 sets of lists, which I am going to have to run 5 different threads processing these 5 lists. I also want to show progress on them in a datagrid which is going to require one "combined" list for binding.

Not sure where to start on this one. If I use concat then if I remove an item on the sublist, the parent list will not be updated.

public partial class MainWindow : Window
{


    ObservableCollection<TestObject> _combinedList = new ObservableCollection<TestObject>();
    public ObservableCollection<TestObject> CombinedList
    {
        get
        {
            return _combinedList;
        }
        set
        {
            _combinedList = value;
        }
    }


    List<TestObject> _list1 = new List<TestObject>();
    List<TestObject> _list2 = new List<TestObject>();
    List<TestObject> _list3 = new List<TestObject>();
    List<TestObject> _list4 = new List<TestObject>();
    List<TestObject> _list5 = new List<TestObject>();


    public MainWindow()
    {
        InitializeComponent();

        dataGridCombined.ItemsSource = CombinedList;

    }

    private void cmdCreateLists_Click(object sender, RoutedEventArgs e)
    {
        createLists();
    }

    public void createLists()
    {
        for (int i = 1; i < 6; i++)
        {
            _list1.Add(new TestObject(i));
        }

        for (int i = 1; i < 6; i++)
        {
            _list2.Add(new TestObject(i*3));
        }

        for (int i = 1; i < 6; i++)
        {
            _list3.Add(new TestObject(i*5));
        }

        for (int i = 1; i < 6; i++)
        {
            _list4.Add(new TestObject(i*7));
        }

        for (int i = 1; i < 6; i++)
        {
            _list5.Add(new TestObject(i*9));
        }

    }

    private void cmdMergeLists_Click(object sender, RoutedEventArgs e)
    {
        //Doesn't work and if i remove item from sublist, CombinedList doesn't update.
        CombinedList = _list1.Concat<TestObject>(_list2);


    }

}

public class TestObject : BindableObject
{
    private int _number;
    public int Number
    {
        get
        {
            return _number;
        }
        set
        {
            _number = value;
            RaisePropertyChanged("Number");
        }
    }

    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
            RaisePropertyChanged("Name");
        }
    }

    public TestObject(int inNum)
    {
        _number = inNum;
        _name = "John";
    }

}

Am I going to have to write some sort of custom collection here ?

Regards

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

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

发布评论

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

评论(4

独木成林 2024-12-23 20:29:20

首先将所有项目添加到 ObservableCollection 中。这将为您提供合并列表。

如果我使用 concat,那么如果我删除子列表上的项目,父列表将不会更新。

您可以订阅ObservableCollection.CollectionChanged< /a> 事件用于“组合”集合,然后在发生更改时从适当的后备集合中添加或删除。

Start by adding all items to the ObservableCollection<T>. This will provide you the combined list.

If I use concat then if I remove an item on the sublist, the parent list will not be updated.

You can subscribe to the ObservableCollection<T>.CollectionChanged event for the "combined" collection, and then add or remove from the appropriate backing collection as changes occur.

一袭白衣梦中忆 2024-12-23 20:29:20

使用 AddRange 与单独调用 Add()

或者,只让所有线程都可以访问一个集合,然后从每个线程向该集合添加内容,而不是组合 5 个单独的集合。

Use AddRange vs. individual calls to Add()

Or, just have one collection accessible by all the threads and add to it from each of them instead of combining 5 separate collections.

靑春怀旧 2024-12-23 20:29:20

您可以对其进行“带注释”串联:

var combined = list1.Select(i => new {
        Item = i,
        Origin = list1 }).Concat(
  list2.Select(i => new {
        Item = i,
        Origin = list2 }))
 .ToList(); // TODO extend for more lists

这样,您可以获得项目源列表:

combined[13].Origin.Remove(combined[13]);
// refresh combined list!

You could make a 'annotated' concatenation of it:

var combined = list1.Select(i => new {
        Item = i,
        Origin = list1 }).Concat(
  list2.Select(i => new {
        Item = i,
        Origin = list2 }))
 .ToList(); // TODO extend for more lists

That way, you can get an items source list:

combined[13].Origin.Remove(combined[13]);
// refresh combined list!
墨小墨 2024-12-23 20:29:20

只需使用 CompositeCollection 即可。您可以添加任何项目,包括其他集合,并且绑定到它的控件将收到其中所有项目的平面视图。

Just use CompositeCollection. You can add any item, including others collections, and controls that are bound to it will receive a flattened view of the all items within.

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