0
votes

I have a DataGrid that has AutoGenerateColumns="True". I'm binding that DataGrid to a collection of objects that are of an unknown datatype at design time.

The DataGrid is able to identify the data type that a particular column is bound to. It shows a textblock or a checkbox if the content is text or a bool respectively.

I want to show data in a particular way (i.e. a DataTemplate) if it's of a specific type. Is there a way to do this?

1
Do you know how many columns you need? Disregarding the DataType? - Aaron McIver
No, I may bind to an object that has 8 properties or one that has 25. All different datatypes for the properties. If a datatype is a "RickLinkAction", I'd like to display a LinkButton. If it's a String, I'll want to display a TextBlock. If it's a "RickCalendarAction" I'll want to display something else. - Rick Kierner

1 Answers

0
votes

Since you are unaware of the columns which may exist you have to revert to code behind. If you had known that you will always have 8 properties on the given object along with there types; you could move this behavior into the XAML and not have to deal with the handling of the event.

The MSDN reference sums up how to accomplish this but it boils down to handling the AutoGeneratingColumn event.

private void dataGrid1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{

     ...

}