简体   繁体   中英

WPF DataGrid disabled row Foreground color not beeing set

I face a problem with WPF DataGrid.

I want to set the Foreground value of a disabled DataGrid row, but the Foreground stays always gray.

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.

Here is what I get:

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

As you can see, the Background is applied, but the Foreground not (for row with Deleted State)

Define a custom DataGridCell style:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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