选择行时设置文本WPF DataGrid行的颜色

问题描述 投票:12回答:2

我正在尝试更改WPF数据网格中所选行中的文本颜色。默认情况下它会更改文本颜色白色有没有办法使用样式/触发器等来改变它?

提前致谢!

wpf datagrid
2个回答
25
投票

试试这个

<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}" >
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Foreground" Value="Green"/>
        </Trigger>
    </Style.Triggers>
</Style>

然后你可以在你认为合适的列中使用它

<DataGrid ...>
    <DataGrid.Columns>
        <DataGridTextColumn CellStyle="{StaticResource DataGridCellStyle}" .../>

如果要将其应用于所有列,可以将样式的x:键更改为

<Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}" >

0
投票

如果要完全删除Foreground颜色更改(例如,如果DataGrid对于不同的行具有不同的颜色),则可以执行以下操作:

    <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=Foreground}" />
            </Trigger>
        </Style.Triggers>
    </Style>

如果要为此样式指定名称(如上一个答案中所示),请添加x:Key。

© www.soinside.com 2019 - 2024. All rights reserved.