Gregor Leban
Gregor Leban

Reputation: 552

How to use two templates for ListViewItem using WPF

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

alt text http://img401.yfrog.com/img401/1719/inboxy.png

Upvotes: 1

Views: 794

Answers (2)

gehho
gehho

Reputation: 9238

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.

Upvotes: 1

SysAdmin
SysAdmin

Reputation: 5575

You should check out DataTemplateSelector. just google it.

The idea is this.

  1. You will define 2-3 templates in XAML
  2. create a class derived from DataTemplateSelector and based on a field/ typeof object you will return the appropriate template

Upvotes: 2

Related Questions