简体   繁体   English

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

[英]WPF DataGrid disabled row Foreground color not beeing set

I face a problem with WPF DataGrid. 我遇到了WPF DataGrid的问题。

I want to set the Foreground value of a disabled DataGrid row, but the Foreground stays always gray. 我想设置禁用的DataGrid行的“ Foreground值,但“ Foreground始终保持灰色。

Here is the code I use: 这是我使用的代码:

<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>

Added , Changed and Deleted are just enum values. AddedChangedDeleted只是枚举值。

Here is what I get: 这是我得到的:

具有红色背景的行的错误的前景颜色

As you can see, the Background is applied, but the Foreground not (for row with Deleted State) 如您所见, Background被应用,但Foreground未被应用(对于具有“ Deleted状态”的行)

Define a custom DataGridCell style: 定义自定义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>

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

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