组合多个列表
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先将所有项目添加到
ObservableCollection
中。这将为您提供合并列表。您可以订阅
ObservableCollection.CollectionChanged
< /a> 事件用于“组合”集合,然后在发生更改时从适当的后备集合中添加或删除。Start by adding all items to the
ObservableCollection<T>
. This will provide you the combined list.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.使用 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.
您可以对其进行“带注释”串联:
这样,您可以获得项目源列表:
You could make a 'annotated' concatenation of it:
That way, you can get an items source list:
只需使用 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.