In our data grid we're using an ItemTemplateSelector
to switch between two data templates based on the data bound to a particular cell.
As the number of columns depends on the current data set we're using AutoGenerateColumns
in our DataGrid
.
It appears that this particular combination does not work well -
the template selector isn't even called.
Can we use the template selector in a data grid where columns are created automatically?
More specifically: Is this possible using XAML only w/o putting logic into the code-behind file or using custom behaviours?
Our data grid definition is fairly trivial:
<DataGrid
ItemTemplateSelector="{StaticResource myCustomDataTemplateSelector}"
ItemsSource="{Binding MyData}">
<DataGrid.Columns>
</DataGrid.Columns>
</DataGrid>
The item template selector definition:
<UserControl.Resources>
<ResourceDictionary>
<helpers:CustomDataTemplateSelector x:Key="myCustomDataTemplateSelector"/>
</ResourceDictionary>
</UserControl.Resources>
I recently ran into this problem and solved it this way:
we can inherit the class
DataGridBoundColumn
Next, in the event handler, where the column is generated, I do:
In the resources of the window I have the corresponding templates.
It is possible to do through DataTemplateSelector, but there was no time.