tags:

views:

153

answers:

2

Lets say the model looks like this: (plants are easier to describe than telephony system acronyms)

class CornAttribs
    dim numKernels

class OnionAttribs
    dim numLayers
    dim color

class Vegetable
    Implements INotifyPropertyChanged
    ....snip notify related stuff
    dim name as string
    dim attrib as object  'point to Corn or Onion

During runtime the data could look like this:

Vegetables:
    "corn1", numKernels = 60
    "onion1", layers = 10, color = white
    "corn2", numKernels = 80

In my form I want to have a grid that is bound to a CollectionView to shows certain named vegetables. Lets just say there is no filter on the view for this example, but there could be. I want the grid output to look like this:

===========================
Onion   |  Layers  | Color
--------------------------
onion1  |     10   | white
===========================
Corn   |  Kernels  
--------------------------
corn1  |     60 
corn2  |     80
==========================

There is a limited number of "attrib" objects so I would have distinct markup for each grid.

Is OK to continue down this path or if I should have all 3 objects be collections and have the name attribute in each and remove the attrib member in Vegetable? (this is my current implementation, however the "Model" code could be simplified if I am able to use the above)

My question sort of boils down to: At each change of the type of object pointed to by the attrib member, can WPF switch grid column definitions and also display a new header?

Thanks in advance.

+4  A: 

Yes.

Check out this Dr. WPF article in the section "Using a Type-Specific Data Template". It's pretty easy to implement. http://www.drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx

Anderson Imes
The DataTemplateSelector would allow me to choose a template via type, however I need a header at each type change. It is almost as if I am "grouping".What about GroupStyle.HeaderTemplate? The column layout needs to be different for each group but could I not use DataTemplateSelector for both the GroupStyle.HeaderTemplate and the items?
wtjones
A: 

No, no, absolutely not.

DataGrid only can single header row and single column set. It just doesn't support the feature you want.

That's the same for both DataGrid and ListView.

Oleg Mihailik