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

StackOverflow https://stackoverflow.com/questions/1816533

  •  08-07-2019
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top