2

I use a DataGrid (WPF 4.0) control. The style for it is placed in a ResourceDictionary and contains a nested Style element:

<Style x:Key="MyDataGridStyle" TargetType="{x:Type controls:DataGrid}">
    <Setter Property="Background" Value="Black"/>
    <Setter Property="CellStyle">
        <Setter.Value>
            <Style TargetType="{x:Type DataGridCell}">
                <Setter Property="BorderThickness" Value="0" />
                <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            </Style>
        </Setter.Value>
    </Setter>
</Style>

Here, only the Background style is applied. The CellStyle NOT.

It only works when I define the CellStyle directly inside the DataGrid element:

<DataGrid Style="{StaticResource MyDataGridStyle}">
       <DataGrid.CellStyle>
            <Style TargetType="{x:Type DataGridCell}">
                <Setter Property="BorderThickness" Value="0" />
                <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            </Style>
       </DataGrid.CellStyle>
</DataGrid>

Any ideas why?

Update

It seems that there is a problem when mixing styles with PresentationFramework.Aero theme that I have referenced in my ResourceDictionary. If I remove the reference it works!

mamuesstack
  • 1,111
  • 2
  • 16
  • 34
  • Separate the style into two. – David Apr 11 '13 at 12:49
  • Ok, but is there an explanation? I also can't reference Styles whithin a ResourceDictionary: e.g. `` won't work, even if I use `DynamicResource` instead or defining MyDataGridCellStyle before MyDataGridStyle. Right now I need to define each Style for the DataGrid within the DataGrid element (Style, ColumnHeaderStyle, ItemContainerStyle, CellStyle ...) – mamuesstack Apr 11 '13 at 13:07

1 Answers1

1

Doing it the way you suggest works fine for me, I'll paste the code that I used to test your style. (note that I added background = red so I could actually see if the style was being applied)

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="MyDataGridStyle" TargetType="{x:Type DataGrid}">
            <Setter Property="Background" Value="Black"/>
            <Setter Property="CellStyle">
                <Setter.Value>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="BorderThickness" Value="0" />
                        <Setter Property="Background" Value="Red"/>
                        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <DataGrid Style="{StaticResource MyDataGridStyle}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Text}"/>
            <DataGridTextColumn Binding="{Binding Tag}"/>
        </DataGrid.Columns>
        <DataGrid.Items>
            <TextBlock Text="Item1" Tag="aa"/>
            <TextBlock Text="Item2" Tag="bb"/>
            <TextBlock Text="Item3" Tag="cc"/>
            <TextBlock Text="Item4" Tag="dd"/>
        </DataGrid.Items>
    </DataGrid>
</Window>

the results are

enter image description here

Andy
  • 6,366
  • 1
  • 32
  • 37
  • Have your also tried to place the styles in a separate ResourceDictionary? – mamuesstack Apr 11 '13 at 13:24
  • Yes it works just fine. Tried in both App.xaml and in another resource dictionary that I linked in the MainWindow.xaml. – Andy Apr 11 '13 at 13:27
  • It seems that there is a problem when mixing styles with PresentationFramework.Aero theme. If I remove this theme from my MergedDictionaries it works! – mamuesstack Apr 11 '13 at 13:40