WPF DataGrid禁用行前景颜色未设置

时间:2018-02-14 15:22:05

标签: c# wpf

我遇到了WPF DataGrid的问题。

我想设置已禁用的Foreground行的DataGrid值,但Foreground始终为灰色。

以下是我使用的代码:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <EventSetter Event="MouseDoubleClick" Handler="DataGridRowDoubleClick" />
        <Setter Property="IsEnabled" Value="{Binding Path=IsMD4Valid}" />
        <Setter Property="IsHitTestVisible" Value="{Binding Path=IsMD4Valid}" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding State}" Value="Added">
                <Setter Property="Background" Value="DarkGreen"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="FontWeight" Value="Bold"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding State}" Value="Changed">
                <Setter Property="Background" Value="DarkBlue"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="FontWeight" Value="Bold"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding State}" Value="Deleted">
                <Setter Property="IsEnabled" Value="false" />
                <Setter Property="IsHitTestVisible" Value="false" />
                <Setter Property="Background" Value="DarkRed"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="FontWeight" Value="Bold"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

AddedChangedDeleted只是枚举值。

这是我得到的:

wrong Foreground color for rows with red Background

如您所见,Background已应用,但Foreground没有(对于Deleted州的行

1 个答案:

答案 0 :(得分:2)

定义自定义DataGridCell样式:

<DataGrid.CellStyle>
    <Style TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{Binding Foreground, RelativeSource={RelativeSource AncestorType=DataGridRow}}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.CellStyle>