WPF 合并项目源列表
我正在后端类中处理 2 个列表。每个列表都是不同的类型。希望向用户呈现一个列表(包含两个列表的并集),当选择此列表中的项目时,会显示该项目的详细信息。
代码看起来像这样:
我的后端类看起来像这样
public ObservableCollection<Person> People {get;}
public ObservableCollection<Product> Products {get;}
我的 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>
有什么建议吗?
I am working with a 2 lists in a backend class. Each list is a different type. Would like to present the user with a single list (containing a union of both lists) of which when an item in this list is selected the item's details appear.
The code will look something like:
My backend class looks somethings like this
public ObservableCollection<Person> People {get;}
public ObservableCollection<Product> Products {get;}
My XAML Looks Something Like This
<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>
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此,您可以使用 CompositeCollection。看看这个问题。
You can use a CompositeCollection for this. Have a look at this question.
我不明白为什么你不只是在 ViewModel 中公开这样的属性:
然后在你的 xaml 中执行以下操作:
更新:
如果你需要以不同的方式操作你的模型,请执行以下操作:
这只是一个例子。但如果您修改上述内容以满足您的需求,它可能会帮助您实现目标
I don't understand why you don't just have expose a property like this in your ViewModel:
and then in your xaml you do this:
UPDATE:
If you need to manipulate your model differently do the following:
This is just an example. But if you modify the above to meet your needs, it might get you to your goal
我在此处找到了一篇博文,它让我了解了大部分内容。我使用作者 AggregateCollection 和多值转换器来完成工作。
I found a blog post here that got me most of the way. I used the authors AggregateCollection along with a multivalueconverter to get the job done.