如何使用 WPF 对 ListViewItem 使用两个模板
我想要一个 ListView,其中包含类似于 Microsoft Outlook 收件箱的项目,其中项目按对话排列(请参阅所附照片)。项目可以是包含对话主题的简单文本块(在照片上是蓝线),也可以包含 ListView 标题指定的电子邮件信息。如果带有对话主题的项目是按钮,则最好通过按此按钮来显示或隐藏该对话中的电子邮件。
我有什么想法可以实现这一目标吗?我是否指定两个 ListViewItem 模板?如果是,我如何在代码中告诉每个项目使用哪个模板?
任何帮助将不胜感激! 问候, Gregor
I would like to have a ListView that would have have items similar to Microsoft Outlook inbox with items arranged by Conversations (see the attached photo). An item can either be a simple textblock containing the topic of the conversation (on the photo this are the blue lines) or it can contain email information specified by the ListView's header. It would be best if the items with conversation topics would be push buttons so that emails inside that conversation could be shown or hidden by pressing this button.
Any ideas how can I achieve this? Do I specify two ListViewItem templates? If yes, how do I tell in the code which template to use for each item?
Any help will be GREATLY appreciated!!
Regards,
Gregor
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该查看 DataTemplateSelector。只需谷歌一下即可。
想法是这样的。
您将返回适当的模板
You should check out DataTemplateSelector. just google it.
The idea is this.
you will return the appropriate template
屏幕截图中使用的概念称为分组。如果您有
,则可以轻松使用分组ListCollectionView
作为您的 ItemsSource。然后,您需要为ListCollectionView 的 GroupDescriptions 属性。要定义这些组的外观,您可以查看此帖子:如何对 WPF ListView 中的项目进行分组。其他感兴趣的博客文章可能是:Bea Stollnitz:如何进行自定义分组? 和 Bea Stollnitz:如何对数据项组进行排序?
在这种情况下,您可以使用电子邮件对话的标题作为组标题,也许还可以使用一些附加数据。
The concept used in your screenshot is called Grouping. You can easily use grouping if you have a
ListCollectionView
as your ItemsSource. You then need to specify several GroupDescriptions for the ListCollectionView's GroupDescriptions property.To define how these groups look like, you might have a look at this SO post: How do I group items in a WPF ListView. Other blog posts of interest might be: Bea Stollnitz: How can I do custom Grouping?, and Bea Stollnitz: How do I sort groups of data items?
In your scenario you would use the title of your email conversation as the group header, and maybe some additional data.