App.xaml に DataGrid スタイルがあります。
<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>
これは、アプリケーション内のすべてのデータグリッドでうまく機能します。ただし、データグリッドの 1 つで、特定の列が同じ値を共有している場合、行をグループ化したいと考えています。したがって、その特定のデータグリッドで次を使用します。
<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>
問題:この DataGrid は、テキスト (前景) を私のスタイルではなく黒で表示することを除いて、私の DataGrid スタイルに基づいてすべてを正しく表示します。
解決策: ItemsPresenter を次のいずれかに変更することで、問題を解決できます (なぜこれが必要なのかわかりません)。
<ItemsPresenter TextElement.Foreground="{StaticResource DataGridItemTextBrush}"/>
また
<ItemsPresenter TextBlock.Foreground="{StaticResource DataGridItemTextBrush}" />
質問:なぜこれが起こるのか説明できますか、また、ItemsPresenter が DataGrid スタイルをオーバーライドしないことを保証するより良いソリューションを提供できますか?
ありがとうございました!