How to use two templates for ListViewItem using WPF - 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

You should check out DataTemplateSelector. just google it.
The idea is this.
You will define 2-3 templates in XAML
create a class derived from DataTemplateSelector and based on a field/ typeof object
you will return the appropriate template

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.

Related

WPF / MVVM: Show specific image from list of images?

I have two lists of different object types in my ViewModel. For example I'd say the first one is a list of all available images, the second one is a list of all articles. Each article belongs to an article category for which I'd like to show a thumbnail in the GUI when an article is selected and shown. For the sake of a simple example I assume that the Id of the category equals the Id of the corresponding image.
For a ComboBox this would be an easy task: I would assing the list of all images as ItemSource and then the article's category key to the SelectedValue. The SelectedValuePath would have to point to the Id of the image.
However a ComboBox does not make sense here as I only want to display the correct category image - I do not want any editing, a dropdown, etc... How would I solve this with a regular Image-UserControl (or maybe a similar UserContro)?
BTW: Right now I have to use an additional Property on my Article class which directly holds the correct image, but I think there should be a better way to solve this issue.
Any ideas?
Regards
ralf
There's no problem with creating a get-only property on your ViewModel that provides the image that the view should bind to. The whole point of ViewModels is to shape model data into a structure suitable for binding views to.

Dynamic Databinding in WPF

I am filling up a Stackpanel with Textboxes. Each textbox, Should display the Value Property of a Class i have written.
The number of Textboxes is always different so i can't create a databinding in the xaml file.
Are there tutorials, that show how can i archive my goal?
Edit: Thanks for the replies so far. After reading the articles i still not clear how to implement my Problem.
Here is what I'm trying to achieve:
I have an SQL Server DB and there a Table with an ID and a Value.
I now load all Values and IDs i want to display into my application. (number of loaded rows is always differnt).
I need to display a textbox for each row and display the value there and after the value is changed i write the value back to the database.
I don't know how I should query the data and than bind it to the textboxes.
I don't need an implementations, I am happy with every piece of advice I can get.
yes there are a number of tutorials out there. It sound like you are interested in datatemplating.
I would suggest looking Here on MSDN. A few tutorials on databinding in general may be useful DataBinding on MSDN
If your class, classValue, has a public property Value then just create a List and bind that to a ListBox with the item DisplayValuePath = Value. Repeater controls are used to bind to collections.

applying images to a dynamic data listbox in xaml

I'm new to xaml and wpf. I actually wanted to create a dynamic listbox populating a list of customers with images ('redball' for old customers and 'greenball' for new customers) adjacent to the names in the list. The list as I mentioned will be having random number of entries. Kindly suggest me or guide me trough any samples or blogs which might be helpful.
Thanks
if your customer object has a public property (IsNew or something like that) then you can simply use a datatemplate with Style.Datatrigger to show a green or red ball. if you a new to wpf you should read some books to bwecome familiar with bindings, xaml styles and so on. and if you did this you should read about mvvm pattern.

How do I set a dynamic image in WP7 LongListSelector in GroupHeaderTemplate?

I am using the LongListSelector from the Silverlight for Windows Phone Toolkit.
For each group I would like to show a small group related icon, in the group header.
Group A
Item 1
Item 2
Item 3
Each of the items have the same key, and are grouping correctly. And each item has the same image. But when I change my GroupHeaderTemplate from the key to something else. It doesn't seem to handle this correctly.
Typically you see examples that use a "Group" object which is based on the IGrouping<TKey, T> style object which implements IEnumberable and has a single Key property.
However there is no reason why your group object can't also have a whole range of other properties like "Name" and "ImageSource" whatever that you will be able to bind in the group item templates.
Take a look # these two articles for insights on LongListSelector
WP7 LongListSelector in depth | Part1: Visual structure and API
WP7 LongListSelector in depth | Part2: Data binding scenarios
Can you show us some more information with regards to your source data and the XAMl that you're using. It sounds like you need to expose a path to the image you want for each group and then bind the Image element in the GroupHeaderTemplate to that property.

How to display in each ListBoxItem textblocks with binding to a filelds of a different tables

Greetings to all and sorry for my English!
I have a ListBox, it's ItemsSource = myClientsList.DefaultView. The Items of ListBox have a template (ControlTemplate), that is defined in a in a separate resource file.
Every Item contains a little TextBlock's, Text -property of each have a binding to fields of my Object myClientsList.
I need to add in a this item template more TexBlock's and each of them must have binding to fields of another my class myOrdersList. - (So I wish to view on each line of ListBox information from different tables of my database - this is a question).
Problem in that that ListBox's ItemsSource have a link to object myClientsList and I cann't set myOrderList to ItemSource of same ListBox. So i must find a way to specify TextBlock.DataContext wich inside ControlTemplate or how it's possible to solve this problem in another way?
p.s. I'm a new in .Net and WPF and probably have a mistakes in my explanation - sorry for it.
It sounds like you have a DataGrid type of display and want to add more columns in order to display the order information for a given client. If this is the case, you are going to have to do a couple of things. First, you will need to create a composite object that stores information for both entities into a single object (so each row of your control has all the data it needs to display). Secondly, I would recommend using an actual DataGrid control to display rows instead of templating a ListBoxItem. The ListView with a GridView built into the framework isn't great, so I would recommend the WPFToolkit's DataGrid for a free option.
There are two issues here, if I've understood the question: how do you create a single collection containing both Clients and Orders, and how do you display Clients and Orders in different ways within the same ListBox?
Regarding the first, you can do this using a CompositeCollection.
Regarding the second, define two DataTemplates instead of a ControlTemplate. As the key of each DataTemplate, use the type of the object it is going to present e.g.
<DataTemplate x:Key="{x:Type local:Client}">
Alternatively, use ItemsControl.ItemTemplateSelector to explicitly point at different DataTemplates depending on the type of item. Ot if you really have to use ControlTemplates, check out ItemsControl.ItemContainerStyleSelector.

Resources