Implicit ElementStyle for DataGridTextColumn?

Oct 13, 2008 at 5:10 PM
Because all the datagrid column types derive from DependencyObject rather than FrameworkElement or FrameworkContentElement, I can't target them for implicit styles. Which seems to lead to silly code like this:

<dg:DataGrid.Columns>
    <dg:DataGridTextColumn Header="A" ElementStyle="{StaticResource ExplicitElementStyle}" />
    <dg:DataGridTextColumn Header="B" ElementStyle="{StaticResource ExplicitElementStyle}" />
    <dg:DataGridTextColumn Header="C" ElementStyle="{StaticResource ExplicitElementStyle}" />
</dg:DataGrid.Columns>

Surely there must be a better way. What am I missing?
Coordinator
Oct 20, 2008 at 12:21 PM
From a high-level perspective, DataGridColumns are used by the DataGrid as intermediate data holders for what and how to render cells and display data in cells.  They are not visuals and hence they do not derive from FrameworkElement.  They do derive from DependencyObject so they can consume services such as data binding and property change notifications.  GridViewColumns for a ListView are also DependencyObjects which means they too are unable to consume servies such as implicit styles. 

A possible workaround is to create an implicit style on the visual element of the column.  In your case you can create an implicit style on a TextBlock.  If you have TextBlocks in other parts of your application you can always delcare the implicit style in the scope of just the DataGrid.