0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WPF DataGridのheader Foreground を変更するには

Last updated at Posted at 2025-03-21

ありがちな解説だと

    <Style TargetType="DataGridColumnHeader">
        <Setter Property="Foreground" Value="Red" />
    </Style>

などとするように書いてあるが、単純にバインドした DataGrid ではこれでは変更できない。

ChatGPT による解説

通常、DataGrid.ColumnHeaderStyle で Foreground を設定すれば DataGridColumnHeader に適用されますが、ヘッダーのコンテンツ (Content) が string の場合、ContentPresenter によって描画されるため、Foreground が適用されません。
そのため、TextBlock を明示的に Header に入れる必要があります。

したがってheader構造を直接指定する必要がある。

            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Name}">
                    <DataGridTextColumn.Header>
                        <TextBlock Text="Name" Foreground="White"></TextBlock>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
            </DataGrid.Columns>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?