WPF合并ItemsSource列表

Gus*_*Gus 4 wpf binding listbox mvvm itemssource

我正在后端类中使用2个列表.每个列表都是不同的类型.想要向用户显示单个列表(包含两个列表的并集),当选择此列表中的项目时,将显示项目的详细信息.

代码看起来像:

我的后端课看起来像这样的事情

public ObservableCollection<Person> People {get;}
public ObservableCollection<Product> Products {get;}
Run Code Online (Sandbox Code Playgroud)

我的XAML看起来像这样

<ListBox x:Name="TheListBox" ItemsSource={Some Expression to merge People and Products}>
   <ListBox.Resources>
         People and Product Data Templates
   </ListBox.Resources>
</ListBox>
      ...
<ContentControl Content={Binding ElementName=TheListBox, Path=SelectedItem }>
   <ContentControl.Resources>
         Data Templates for showing People and Product details
   </ContentControl.Resources>
</ContentControl>
Run Code Online (Sandbox Code Playgroud)

有什么建议?

Chr*_*sto 8

您可以使用CompositeCollection.看看这个问题.


Gus*_*Gus 0

我在这里找到了一篇博文,它让我了解了大部分内容。我使用作者 AggregateCollection 和多值转换器来完成工作。