简体   繁体   English

如何更改所选WPF DataGrid行标题的颜色

[英]How to change the colour of a selected WPF DataGrid row header

I have a WPF DataGrid whose row headers get filled with (a small amount of) text, and this works great... until you highlight the row, when the text turns white and is barely visible: 我有一个WPF DataGrid,它的行标题充满了(少量)文本,这很好用...直到您突出显示该行,当文本变成白色并且几乎不可见时:

在此处输入图片说明

In an attempt to force it to stay black, I set the following code containing both an IsSelected trigger and an explicit DataTemplate with its colour also set. 为了使它保持黑色,我设置了以下代码,其中同时包含一个IsSelected触发器和一个显式的DataTemplate及其颜色。

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="Black"/>
            </Trigger>
        </Style.Triggers>
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Foreground="Black" Text="{Binding}" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGrid.RowStyle>

Unfortunately, neither of these has any effect. 不幸的是,这些都不起作用。 The text remains stubbornly white until the row is deselected. 文本将顽固地保持为白色,直到取消选择该行。

How can I force this text to remain black (or any other colour) when selected? 选择后如何强制该文本保持黑色(或任何其他颜色)?

The style trigger is defined on the row itself instead of the header's template item. 样式触发器是在行本身而不是标题的模板项上定义的。 Simply move the trigger definition into the header template would do the trick. 只需将触发器定义移动到标头模板即可解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM