Take the 2-minute tour ×

I just converted a WPF project from .NET 3.5 to .NET 4.0.

I'm now using the .NET 4.0 DataGrid control rather than the WPF Toolkit DataGrid control. Functionally, everything is still working, but my styles are not applying as expected.

As you can see from the below screen captures, the alternating row formatting, padding, bold headings, etc. have stopped working.

Before (WPF Toolkit DataGrid)

Screen capture of WPF Toolkit DataGrid

After (.NET 4.0 DataGrid)

Screen capture of .NET 4.0 DataGrid

Here is my entire resource dictionary.

    <Style x:Key="DataGrid_ColumnHeaderStyle" TargetType="DataGridColumnHeader">
        <Setter Property="TextBlock.TextAlignment" Value="Center" />
        <Setter Property="TextBlock.TextWrapping" Value="WrapWithOverflow" />
    <Style x:Key="DataGrid_CellStyle" TargetType="DataGridCell">
        <Setter Property="Padding" Value="5,5,5,5" />
        <Setter Property="Template">
                <ControlTemplate TargetType="DataGridCell">
                    <Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}">
                        <ContentPresenter />
    <Style TargetType="DataGrid">
        <Setter Property="ColumnHeaderStyle" Value="{StaticResource DataGrid_ColumnHeaderStyle}" />
        <Setter Property="CellStyle" Value="{StaticResource DataGrid_CellStyle}" />
        <Setter Property="AlternatingRowBackground" Value="#F0F0F0" />
        <Setter Property="VerticalGridLinesBrush" Value="LightGray" />
        <Setter Property="HeadersVisibility" Value="Column" />
        <Setter Property="SelectionMode" Value="Single" />
        <Setter Property="SelectionUnit" Value="FullRow" />
        <Setter Property="GridLinesVisibility" Value="Vertical" />
        <Setter Property="AutoGenerateColumns" Value="False" />
        <Setter Property="CanUserAddRows" Value="False" />
        <Setter Property="CanUserDeleteRows" Value="False" />
        <Setter Property="CanUserReorderColumns" Value="True" />
        <Setter Property="CanUserResizeColumns" Value="True" />
        <Setter Property="CanUserResizeRows" Value="False" />
        <Setter Property="CanUserSortColumns" Value="True" />
        <Setter Property="IsReadOnly" Value="True" />
        <Setter Property="BorderBrush" Value="#DDDDDD" />
        <Setter Property="HorizontalGridLinesBrush" Value="#DDDDDD" />
        <Setter Property="VerticalGridLinesBrush" Value="#DDDDDD" />
    <Style x:Key="DataGrid_FixedStyle" TargetType="DataGrid" BasedOn="{StaticResource {x:Type DataGrid}}">
        <Setter Property="CanUserReorderColumns" Value="False" />
        <Setter Property="CanUserResizeColumns" Value="False" />
        <Setter Property="CanUserSortColumns" Value="False" />

Here is a usage example (note that the style is set to "DataGrid_FixedStyle"):

    Style="{StaticResource DataGrid_FixedStyle}"
    Grid.Column="0" Foreground="Black"
    SelectedIndex="{Binding SelectedParticipantIndex, Mode=TwoWay}"
    ItemsSource="{Binding Participants}">
        <DataGridTextColumn Foreground="Black" Header="Participant" Binding="{Binding ParticipantId}" />


To make sure the resource dictionary was really being used, I added the following setter to the <Style TargetType="DataGrid">...</Style>:


As you can see from the screen capture below, the font size is cartoonishly large, so the style itself is definitely not being ignored. The problem is that many of the settings are not being used or not working for some reason.

alt text

Any theory on what might have caused my styles to break?

share|improve this question
I just tried putting the contents of your ResourceDictionary into Window.Resources in the same file as your DataGrid example and it worked fine. How are you including your ResourceDictionary? –  Colin Thomsen Nov 21 '10 at 1:51
@Colin, strange, it doesn't work for me no matter where I put the resource dictionary. –  devuxer Nov 21 '10 at 6:38
Have you tried setting x:Key on the base style for troubleshooting? <Style x:Key="DataGridBaseStyle" TargetType="DataGrid">. <Style x:Key="DataGrid_FixedStyle" TargetType="DataGrid" BasedOn="{StaticResource DataGridBaseStyle}"> –  Fredrik Hedblad Nov 21 '10 at 8:20
@Meleak, yes, I did try that. It didn't make any difference. Please see my answer below. –  devuxer Nov 21 '10 at 17:48
add comment

1 Answer

up vote 1 down vote accepted

I think I found the culprit. In my App.xaml, I apply the "Aero" theme using the following declaration:

    ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />

After that, I include the resource dictionary that performs additional styling on the DataGrid using the following declaration:

    Source="/CommonLibraryWpf;component/ResourceDictionaries/DataGridResourceDictionary.xaml" />

If I remove the Aero theme, the custom styling applies correctly (although it loses its Aero look since I'm running this on Windows XP). This problem definitely didn't occur in WPF 3.5, though. I'm not sure what exactly has changed between .NET 3.5 and 4.0 that would make this fail.

Now I just have to figure out how to get the Aero theme and the custom DataGrid styling to work at the same time :)


Please see this followup question.

share|improve this answer
add comment

Your Answer


