wpfdatagridstylesitemspresenter

Why does ItemsPresenter override my DataGrid's foreground style?


I have a DataGrid style in App.xaml:

<Style TargetType="{x:Type DataGrid}">
    <Setter Property="Foreground" Value="{StaticResource DataGridItemTextBrush}" />
    <Setter Property="VerticalGridLinesBrush" Value="{StaticResource GridBrush}" />
    <Setter Property="HorizontalGridLinesBrush" Value="{StaticResource GridBrush}" />
    <Setter Property="RowBackground" Value="Transparent" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="HeadersVisibility" Value="Column" />
    <Setter Property="AlternatingRowBackground" Value="#77000000" />
</Style>

This works great for all of my datagrid's in my applications. However, for one of my datagrids, I want to group my rows if a specific column shares the same values. So I use the following on that particular datagrid:

<DataGrid.GroupStyle>
    <GroupStyle>
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Path=Name}" Padding="3"/>
                </StackPanel>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}" >
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander>
                                <Expander.Resources>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="Foreground" Value="White" />
                                    </Style>
                                </Expander.Resources>
                                <Expander.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Path=Name, StringFormat=Set: {0}}" Margin="5,0"/>
                                        <TextBlock Text="{Binding Path=ItemCount, StringFormat=(\{0\} Games)}"/>
                                    </StackPanel>
                                </Expander.Header>
                                <ItemsPresenter />
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</DataGrid.GroupStyle>

Problem: Now this DataGrid displays everything correctly based on my DataGrid style, except it displays the text (foreground) as black instead of my style.

Solution: I can fix the problem (though I don't understand why this is necessary) by modifying my ItemsPresenter to either of the following:

<ItemsPresenter TextElement.Foreground="{StaticResource DataGridItemTextBrush}"/>

or

<ItemsPresenter TextBlock.Foreground="{StaticResource DataGridItemTextBrush}" />

Question: Can anyone explain why this happens and/or offer a better solution that will guarantee that my ItemsPresenter does not override any of my DataGrid styles?

Thank you!


Solution

  • Styling cascades down unless the child opts to override the imposed style. The ItemsPresenter in your case has default values; which you are not overriding within your DataGrid style. Either create an ItemsPresenter style within App.xaml to meet your needs or modify the value locally via an explicit or implicit style, or opt for your proposed solution.

    Also keep in mind that you can use the BasedOn property to inherit the default styling; overriding only certain properties.

    BasedOn="{StaticResource {x:Type DataGrid}}"