views:

59

answers:

2

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.

A: 

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.

itowlson
I have two ObservableCollection Object - myClientsList and myOrdersList. The ListBox work with first one fine, but i need to add to each ListBoxItem some data-fields from second collection.As i understood, i can't use ItemTemplateSelector - because it's a 'selector' and this way will not get me desirable result. I'll try to using a CompositeCollection (I didn't know about presence of this object). Thank to you for your comment.
Alex
A: 

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.

Abe Heidebrecht
Thank you, Abe. I will try to use WPFToolkit's DataGrid as a variant
Alex