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

时间:2010-11-05 09:21:44

标签: wpf datagrid

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

提前致谢!

2 个答案:

答案 0 :(得分: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}" >

答案 1 :(得分: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。

相关问题