I am wondering if it was possible to change the styling of a column in a wpf datagrid depending on the type of item in the ItemsSource collection.
I have a wpf datagrid from the wpf toolkit. The single rows in the grid should be styled depending of the type of item from the ItemsSource collection. So all items are of the same base class type but the columns of some derived types should get a different stylization.
Is this possible?
Thank you :-)
Yes, it is possible to do it in several ways. The one I would go for is writing your own custom "typeswitch" converter that selects a value depending on type of input. Like this:
And then use a binding for the
Style
of the top-level element in template for your cell, and use the above converter for that binding as needed. Here's a simplified example that styles items in aListBox
using it:A WPF-only solution:
I am a little late for the party but if someone else is trying to do something like this, there is a WPF only solution. There are no DataGridColumn class that will directly look for the proper DataTemplate but we can indirectly use a ContentPresenter like this:
By sandwiching a ContentPresenter inside a DataTemplate inside the CellTemplate, we achieve the desired result.