DataGrid - ItemTemplateSelector

Jan 30, 2009 at 11:53 PM
I've been trying to use the ItemTemplateSelector property on the DataGrid, but it doesn't do anything at all. I've tried the same code on a ListView and it works fine. For some reason the DataGrid just ignores its ItemTemplateSelector property. Has this property been implemented yet? If so can someone show me an example of using it? make it do anything, except nothing.

Jan 31, 2009 at 12:57 AM
Hi Thrash,

ItemTemplate and ItemTemplateSelector are inherited properties which are purposely ignored in the DataGrid since they don't really apply to DataGrid in the way that they were meant to in ItemsControl.  Can you explain what you're trying to achieve using these properties?  We can help you find a different way to attain the scenario you're targeting without using these properties.

Jan 31, 2009 at 11:22 PM
I'm trying to dynamically change how cells are displayed depending on certain conditions.

There's a server that sends my WPF application data about the extraction of "archives". When my application first loads up, it connects to the server and asks for the data on all archives. The server sends a nested hash of strings that I convert into a DataTable, that I can then set the DataGrid's ItemSource property to. However, some of the strings that the server sends are coded with a prefix that tells my application that the value is to be displayed in a special way. For example, one of the prefixes is "prog", which says display this cell with a progress bar and textblock on top of it. So one of the strings would look like this: "prog29.51". That says display this as a progress bar and the progress is 29.51%. So when I convert the nested hash into a DataTable, I actually fill the DataTable with a bunch of "CustomCells". A CustomCell has only two properties: "Value" and "DataType. So for "prog29.51", I will create a CustomCell with a value of "29.51" and a datatype of "prog".

When the datagrid is about to display a cell it needs to check to the DataType of the CustomCell and use the appropriate DataTemplate to display it. That's why I was trying to use the ItemTemplateSelector property of the datagrid. The selector would figure out the which DataTemplate to use to display the CustomCell and pass that information on to the datagrid. But, as we know that datagrid ignores that property for some reason. So I tried creative ways to accomplish the same thing just using the ItemTemplate property, until I realized that the DataGrid ignores that property as well. So now I'm not sure what to do...

To clarify the steps:
1. Client starts up
2. Client ask server for data about all the archives
3. Server sends back a nested hash of strings, some contain coded prefixes
4. The Client convert the nested hash into a DataTable containing CustomCells
5. When the DataGrid is about to display a cell it checks the DataType property of the CustomCell
6. The DataGrid then needs to use the correct DataTemplate to display the CustomCell

And just to note: A column's cells may need to use different DataTemplates. For example, the Status column may have one cell that needs to display a progress bar and another cell may need to display an error message.

I hope that's not too confusing...

Feb 2, 2009 at 6:01 PM
Any ideas on this?
Feb 2, 2009 at 11:28 PM
Hi Thrash,

Ok, I think I understand what you're doing.  Have you tried to do this using using Template columns and CellTemplates?  I'm thinking that at your step #5, you should be able to use a Trigger to switch between different templates for your cell (for example a progress bar or textbox) depending on the DataType of the cell.  You might be able to do this through XAML if you can figure out the right binding syntax to check the data type (which would probably be simplest), or in the code behind, you should be able to do something like hook into the event when the column is being generated and apply a template which will switch out the CellTemplate to be the right one for the incoming data type.  Does it sound like something like that might work for you?

Feb 3, 2009 at 6:29 PM
I've tried doing that, but it doesn't seem like that's possible with just triggers. When I look online for this sort of thing people use selectors to do it. Maybe if there were a GeneratingCell event I could get around that, but I don't see one currently...
Feb 5, 2009 at 4:33 PM
Any other ideas on this? I can put this off for another week or two, but ultimately I have to implement this functionality somehow.

Feb 9, 2009 at 6:46 PM
DataGridTemplateColumn has a CellTemplateSelector property which should allow you to do what you're trying to do (sorry, I completely forgot that API existed until one of our developers reminded me). 
Feb 9, 2009 at 8:35 PM
I've been able to assign a DataTemplateSelector to the CellTemplateSelector property of the DataGridTemplateColumn during the AutoGeneratingColumn event and see the results be displayed correctly on the DataGrid. Now I just have to get my real application to implement this. Just a side note though... shouldn't the properties that are not implemented on the DataGrid throw a NotImplementedException?

Thankyou for all your help,